Help! The variable has not been assigned

The script being run is called Block. It is referencing the function Spawn in the script World.
I finally have it referencing correctly. It finds the function, but for some reason the variable Dust (Which has been defined as a GameObject in the World script) is said to be undefined.
I have made the variables public and defined them myself, but it still doesn’t detect it in the Block script.
It works completely fine when you call the function from the World script.
I could just instantiate the GameObject I need every time I run the Spawn command, but that makes the game lag significantly.

This is the code Spawn runs in this condition: else if (block == 17) { Worldgrid[fx][fy][fz] = (GameObject)Instantiate(Dust, new Vector3(fx, fy, fz - 1), Quaternion.Euler(0, 180, 0)); }

Error: UnassignedReferenceException: The variable Dust of World has not been assigned. You probably need to assign the Dust variable of the World script in the inspector. UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, UnityEngine.Vector3 pos, UnityEngine.Quaternion rot) (at <834804db60ae4eb981294088cf979056>:0) UnityEngine.Object.Instantiate (UnityEngine.Object original, UnityEngine.Vector3 position, UnityEngine.Quaternion rotation) (at <834804db60ae4eb981294088cf979056>:0) World.Spawn (System.Int32 block, System.Int32 fx, System.Int32 fy, System.Int32 fz) (at Assets/Scripts/World.cs:1441) Block.OnMouseDown () (at Assets/Scripts/Block.cs:55) UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32)

And again, I have followed the instructions in the error and it doesn’t work.

Try setting a breakpoint in the debugger and seeing what’s going on.

Revisit your assumptions; you might be referencing a different script instance than you think, etc.

This is how I got the reference:

[SerializeField] private GameObject player; The player is where the script World is located.

player.GetComponent<World>().Spawn(17, posx, posy, posz); //Dust This is what executes the Spawn function, specifically the one relating to Dust. That’s why I know it’s referencing the right script, if it wasn’t Dust wouldn’t be mentioned in the error at all. But for some reason it still isn’t defined, even though it is in the original World script on the player.

I meant the whole method might be running in the context of a different object. But there’s not much point in speculating, I really think the debugger would explain a lot. Set a breakpoint and check what this is referring to and the variables in this.

Through the breakpoints, it seems all the variables attached to player are forgotten when being referred to by another script. All GameObjects, around about 40 defined including Dust, are ‘null’. And I cannot just redefine them every time the script is called, that would be dangerously slow. I’m not sure what to do.

But this IS player 1, so it is the correct reference.

All of the GameObjects are defined through Prefabs, if that is important. Looking in each variable, they all contain that error message, but it doesn’t crash the game unless the GameObject is created.

There’s only THREE steps, and as far as I can see you haven’t said:

“This specific variable right here is what Unity is telling me is null.”

Until you do that, you won’t make any progress except by sheer luck.

NullReference is the single most common error while programming. Fixing it is always the same.

  • also known as: Unassigned Reference Exception
  • also known as: Missing Reference Exception
  • also known as: Object reference not set to an instance of an object

The answer is always the same… ALWAYS!

How to fix a NullReferenceException error

Three steps to success:

  • Identify what is null ← any other action taken before this step is WASTED TIME
  • Identify why it is null
  • Fix that

http://plbm.com/?p=221

I did say what was null, I said it after the first person gave their comment. All GameObjects, around about 40 of them, including Dust, are ‘null’ when this script is run this way. I do not have much experience with referencing functions from other files and assume that these just don’t carry over like I hoped. In that case, I really need a simple way to share these variables.