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
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.
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.