Then maybe you changed it or else you have two or more of these scripts in the same scene.
Print the gameObject.name to see.
Also, what’s with all the GetComponent<>T() crazy ninja madness in Awake()? Just drag the references in the good old Unity way until you’re more confident with crazy ninja approaches. See below.
Whatever it is, NullReferenceException is always solved the same way.
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
NullReference is the single most common error while programming. Fixing it is always the same.
Some notes on how to fix a NullReferenceException error in Unity3D:
http://plbm.com/?p=221
Keep in mind that using GetComponent() and its kin (in Children, in Parent, plural, etc) to try and tease out Components at runtime is definitely deep into super-duper-uber-crazy-Ninja advanced stuff.
Here’s the bare minimum of stuff you absolutely MUST keep track of if you insist on using these crazy Ninja methods:
- what you’re looking for:
→ one particular thing?
→ many things?
- where it might be located (what GameObject?)
- where the Get/Find command will look:
→ on one GameObject? Which one? Do you have a reference to it?
→ on every GameObject?
→ on a subset of GameObjects?
- what criteria must be met for something to be found (enabled, named, etc.)
- if your code expects one instance and later you have many (intentional or accidental), does it handle it?
If you are missing knowledge about even ONE of the things above, your call is likely to FAIL.
This sort of coding is to be avoided at all costs unless you know exactly what you are doing.
Botched attempts at using Get- and Find- are responsible for more crashes than useful code, IMNSHO.
If you run into an issue with any of these calls, start with the documentation to understand why.
There is a clear set of extremely-well-defined conditions required for each of these calls to work, as well as definitions of what will and will not be returned.
In the case of collections of Components, the order will NEVER be guaranteed, even if you happen to notice it is always in a particular order on your machine.
It is ALWAYS better to go The Unity Way™ and make dedicated public fields and drag in the references you want.