Help with referencing another script within a script

Hi, I’m developing a game with Unity and I’m having a huge game breaking problem which I just can’t fix. I’m trying to reference a variable(boolean) in a script called “NetworkMaster” but it keeps coming out as null:
```csharp

  • bool myIsLoadingDone;

void Start () {
NetworkMaster nm = (NetworkMaster)this.transform.GetComponent (“NetworkMaster”);

if (nm != null) {
    StartCoroutine ("Wait");

    myIsLoadingDone = true;
    nm.isLoadingDone = myIsLoadingDone;
}

}

IEnumerator Wait() {
yield return new WaitForSeconds (2.5f);
}*
```

“nm.isLoadingDone” is the boolean that I’m trying to change to “myIsLoadingDone”

Is your script attached to an object in the scene?

If it is, you should get a reference to that object then do something like this:

            newButton = GameObject.Instantiate(GameItemPrefab) as GameObject;
            tempScript = newButton.GetComponent<GameItemScript>();

newButton is of type GameObject. GameItemPrefab is a prefab for a component that I am using, but you substitute that line for any game object in your scene.

tempScript is the type GameItemScript.

The important lines is the second line that looks for the script called “GameItemScript” attached to the GameObject “newButton”.

Yes it is attached to an object, but the object is always in the scene, I don’t see why I need to instantiate it. What I’m trying to accomplish is because for some reason I couldn’t put an IEnumerator function in the NetworkMaster script, and I was trying to create a loading period at the start of the scene, so I put it in this script and I was trying to reference it through saying “nm.isLoadingDone” but it says “Object reference not set to an instance of an Object”. The odd thing is the reference line(Line 5) is completely correct.

Try this:

    NetworkMaster nm = (NetworkMaster)GetComponent <NetworkMaster>();

Dang, didn’t work. Nice try

Is it attached to the same object as the script you are using?

If not then you have to find the object before using the getComponent?

Also once you have the variable why don’t you just nm.isLoadingDone rather than place in another variable?

I included the code you need to use:

GameObject.Find("ObjectName").GetComponent<NetworkMaster>();

Yes, “Network Master” and “Loading” script are on the same empty in the scene.

then this example will work, you don’t need the (NetworkMaster) part as you are already putting it into a NetworkMaster

If it isn’t then you need to look at the objects in your scene.

Are both of the scripts on the same Object in your empty scene?

Like I said before, they are, I’ve tried using a public variable and any other method that I could think of. I tried your aforementioned method and it still returned as null. Do you know any other methods for referencing a variable in another script?

make your network variable public at the top and then see if you can drag the object with the NetworkMaster into it. Just to make sure your type is right.

Then work your way up to calling it in the script.

If that works keep it as a public var so you see it in the editor and run again to see if it gets filled.

Saying “it doesn’t work” and giving us not much doesn’t help. The code works fine because I use that a lot. So we need to look at your scene to figure what it is wrong.