NullReferenceException

Kissen

Member
Joined
Jun 20, 2019
Messages
13
Programming Experience
Beginner
I run this code from withing another script and get this error:
NullReferenceException: Object reference not set to an instance of an object
move.Update () (at Assets/Scripts/move.cs:44)

C#:
  GetComponent<Management>().newDayy = true;

Where management is a script and newDayy is a bool.
newDayy is also defaulted a value in the inspector.
 
That likely means GetComponent<Management()> returned null and you were blindly trying to use it.

As an aside, C# code generally code. But I suspect you are actually writing something for Unity, and they have (unfortunately) been calling C# code that is user supplied (as opposed to built into the engine) as scripts.
 
What do you mean it returned null? It is in the scene.

And do you mean that what I referred to as scripts are not actually called scripts?
 
Yep it is for unity. More likely that GetComponent returned null because your Management object is not instantiated. Which is common behaviour for GetComponent
 
580

Is your management object created in a different class?
 
the management "script" is as a separate file in the inspector I don't know what you mean exactly as I'm new to this.
 
Unity is object orientated, Unity inspector is also how you should be wiring up your scripts to your game objects. See your other topic where I briefly touched on this. Why are you referring to a class file ( " Script " ) through code? This defeats the purpose of the object orientated interface you're provided with for doing exactly that.

I suggest you look into some modern Getting Started With Unity 3D Tutorials. Old tutorials won't be as useful as their API has changed alot in recent years.
 
And do you mean that what I referred to as scripts are not actually called scripts?
From the specific Unity programmer's point of view, yes, your code is referred to as "scripts". From the generic C# programmer's point of view, your code is referred to as "code".

The issue here is that you posted your question under the "C# General Discussion", not under something more specific to game programming and/or Unity -- which unfortunately this forum lacks.
 
The issue here is that you posted your question under the "C# General Discussion", not under something more specific to game programming and/or Unity -- which unfortunately this forum lacks.
Which is why one should take the time to provide a FULL and CLEAR explanation of the problem. If it's not implicit in the forum being posted to, even something like the fact that you're using Windows Forms, which is generally what people will assume by default, should be specified explicitly. If you're using something exotic like Unity then that is absolutely something that you should not expect people to divine for themselves. The more people have to guess or assume, the less like the OP is to get the help they want in a timely manner, if at all.
 
Back
Top Bottom