0
\$\begingroup\$

I've got a whole lot of scripts, none of them so far use namespaces. But as I get more and more scripts to deal with I thought it would be a wise idea to start using namespaces. I've never used them before so I'm having a little bit of difficulty. I've declared the namespace in one of my scripts:

using UnityEngine;
using System.Collections;

namespace Train.OnBoardChecker {
    public class OnBoardChecker : MonoBehaviour {
        [SerializeField]private bool isOnBoard;

        public void OnTriggerEnter (Collider col) {
            if(col.gameObject.tag == "Character"){
                isOnBoard = true;
            }
        }

        public void OnTriggerExit (Collider col) {
            if(col.gameObject.tag == "Character"){
                isOnBoard = false;
            }
        }
    }
}

And in my main train script, I've tried to access the 'isOnBoard' bool from the OnBoardChecker script. I was told there were a few ways of doing this, this is what I did:

using UnityEngine;
using System.Collections;
using Train.OnBoardChecker;

namespace Train {
    public class TrainSystems : MonoBehaviour {

        void Start () {

        }

        void Update () {

        }
    }
}

I'm not sure if I'm misunderstanding the whole namespace thing, but I can't access my variables from that script. I've making the variable public, and private (with serialzefield). I've tried these, with the public and private variables, and I even tried making the voids public and not public:

(namespace onboard checker) > OnBoardChecker.OnBoardChecker.insertvariablenamehere
OnBoardChecker.varname

What am I doing wrong, how do I access my variables from the OnBoardChecker script in the TrainSystems script with the namespaces without having to make it public, kind of like the standard assets does.

\$\endgroup\$
1
\$\begingroup\$

You can't access that variable without an actual object of type OnBoardChecker

So you will do something like:

OnBoardChecker myChecker = get some reference to the object/create a new() one

myChecker.isOnBoard; // or if it is private use a get method for it.

What you are trying to do is get a static member of the class. A static member does need a instance of the class to exist.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.