- Home /
NullReferenceException happens only sometimes without cause?
I have two scripts in my game which cycle through objects and do stuff with them (if they exist). The problem is that I've noticed some very strange and frustratingly inconsistent behaviour with array references.
Take the following, which finds the player's base and returns them to it. Sometimes it works, sometimes it doesn't. To my knowledge nothing important changes to cause it to success or fail, it just does that on a whim.
// find home button
if (Input.GetKeyDown(KeyCode.H))
{
GameObject[] home;
home = GameObject.FindGameObjectsWithTag("building");
GameObject goTo = gameObject;
for (int i = 0; i < home.Length; i++)
{
if (home[i].GetComponent<Building>().unitMaster == gameObject)
{
goTo = home[i];
}
}
if (goTo != gameObject)
{
GameObject camera = Camera.main.gameObject;
camera.transform.position = new Vector3(goTo.transform.position.x-14, goTo.transform.position.y+28, goTo.transform.position.z-14);
}
}
The other example suffers the same problem, the error is sometimes thrown that no instance exists when clearly the objects that it is searching for do exist with the right tag etc. What I found even more baffling is that without the Debug.Log prior which refers to the array length it will fail. Otherwise, with the Debug left in, it works. I don't understand that in the slightest.
void Grow2()
{
GameObject[] mushrooms = GameObject.FindGameObjectsWithTag("mushroom");
for (int i = 0; i < mushrooms.Length; i++)
{
// For some bizarre reason it will throw an error about mushrooms[i] not being an instance without this!
Debug.Log ("mushrooms.Length "+mushrooms.Length);
if (mushrooms[i].GetComponent<Mushroom>().height == 2)
{
// mushroom must be able to grow
if (mushrooms[i].GetComponent<Mushroom>().transform.localScale.x < mushrooms[i].GetComponent<Mushroom>().stopSize)
{
// check for collision with non-tile objects (this will stop growth!)
Collider [] colliders = Physics.OverlapSphere(mushrooms[i].transform.position, mushrooms[i].GetComponent<Mushroom>().growFloat * 0.8f);
for(int j = 0; j < colliders.Length; j++)
{
if (colliders[j].tag != "tile" && colliders[j].tag != "unit" && colliders[j].gameObject != mushrooms[i])
{
mushrooms[i].GetComponent<Mushroom>().stopSize = mushrooms[i].GetComponent<Mushroom>().growFloat;
}
}
// grow mushroom
if (mushrooms[i].GetComponent<Mushroom>().transform.localScale.x < mushrooms[i].GetComponent<Mushroom>().stopSize)
{
mushrooms[i].GetComponent<Mushroom>().growScale.x += 0.1f;
mushrooms[i].GetComponent<Mushroom>().growScale.y += 0.1f;
mushrooms[i].GetComponent<Mushroom>().growScale.z += 0.1f;
mushrooms[i].GetComponent<Mushroom>().growFloat += 0.1f;
mushrooms[i].GetComponent<Mushroom>().transform.localScale = mushrooms[i].GetComponent<Mushroom>().growScale;
}
}
}
}
}
I'm at a loss, I don't understand what the problem is since the error seems to happen quite randomly sometimes, and then other times it works without flaw. What could be the cause of the problem?
Debug.Logs are terrible in for loops.
This is because the loop executes much faster than the Debug.Log can keep up, so you only every seem to get the final value and not constant updating values.
Can you provide the specific text and call stack from the NullReferenceException when it occurs?
Answer by tanoshimi · Aug 05, 2014 at 05:59 PM
The only logical explanation of the first error you describe (assuming you've definitely pasted the correct block of code) is either:
There is an object tagged as "building" that does not have a Building script component attached to it.
There is an object tagged as "building" with a Building script component that contains an unassigned/null unitMaster variable
That's it. So you need to step through your code and check where either of those situations could occur.
Didn't modify either code blocks, built the game and ran a multiplayer test; and both things worked during that test, and then when I playtested singleplayer after.
Not really considering this solved since I still have yet to isolate what caused the fault to occur or be fixed in the first place.
Answer by NornIndustries · Aug 11, 2014 at 11:28 PM
So after some testing I have determined that, for reasons I do not understand, sometimes the editor's playtest fails to read arrays. This however is always fixed if I build and run a multiplayer test. I've never seen the error happen in a built version of the game, so I don't know what's causing the issue, but thankfully it isn't crippling.
Sounds like classic shotgun debugging.
NullReferenceExceptions don't just pop up for no reason. You might have a subtle race condition or some other problem, but there definitely is a problem.