- Home /
Null reference that isn't null
I keep getting this error:
NullReferenceException: Object reference not set to an instance of an object
sPlayer.CollisionEvent (UnityEngine.GameObject other) (at Assets/Scripts/Actors/sPlayer.js:111)
sPlayerCollider.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/Actors/sPlayerCollider.js:31)
I have 3 objects here, the player, the players collider (because the mesh won't import into unity with the correct rotation) as a child of the player. Then the enemy.
When the enemy and the players collider collide it triggers the OnTriggerEnter function in the players collider script.
function OnTriggerEnter(other :Collider)
{
if(other != null)
transform.parent.GetComponent(sPlayer).CollisionEvent(other.gameObject);
else
print("other is null");
}
Note there is a null check. this function then calls the real collision function on the parent (player) script and feeds it the enemy object it just collided with.
function CollisionEvent(other:GameObject)
{
if(other != null)
{
if(other.tag == "asteroid")
{
if(shieldActive)
{
playerShield.GetComponent(sShield).DestroyMe();
other.GetComponent(sAsteroid).Damage(transform.position, true);
shieldActive = false;
}
else
{
//Handle hud & game over
theManager.UpdateLives(-1);
other.GetComponent(sAsteroid).Damage(transform.position, true);
}
}
else if(other.tag == "speedpu")
{
other.GetComponent(sPowerup).DestroyMe();
theManager.GetComponent(sManager).TextPopUp("Rapid Fire", Color.green, other.transform.position, other.transform.rotation);
SpeedBoost();
}
else if(other.tag == "shieldpu")
{
theManager.UpdateShieldCount(1);
theManager.GetComponent(sManager).TextPopUp("Shield", Color.blue, other.transform.position, other.transform.rotation);
other.GetComponent(sPowerup).DestroyMe();
}
else if(other.tag == "multipu")
{
other.GetComponent(sPowerup).DestroyMe();
theManager.GetComponent(sManager).TextPopUp("Multi Shot", Color.red, other.transform.position, other.transform.rotation);
MultiShot();
}
}
else
print("no game object found");
}
Note again the null check. It never prints to the console telling me that the object was null, which tells me that there is an object referenced. If I print other, it returns an object reference. It correctly checks the tag of other and goes into the correct conditional, but it never calls any functions on other and never gives an error saying it can't.
Answer by Johnnemann · Apr 28, 2013 at 06:49 AM
The error says it's on line 111 of your file. What is on that line? You're doing a lot of GetComponent().. I suspect that one of those objects doesn't actually have the component that you're looking for.
For example, in this line:
other.GetComponent(sPowerup).DestroyMe();
If other
doesn't have a component sPowerup
, you will get a null reference exception.
Rather than using tags, you could do:
AsteroidComponent:sAsteroid = other.GetComponent(sAsteroid);
if(AsteroidComponent != null)
{
//do asteroid code here
}
PowerUpComponent:sPowerup = other.GetComponent(sPowerup);
if(PowerUpComponent)
{
// do power up code
}
//etc
Turns out I lost a object reference from the inspector, so I just referenced the object via script so i don't have to worry about it anymore. Thanks.
Answer by Eric5h5 · Apr 28, 2013 at 05:11 AM
If you get a null reference exception error, that means 100% of the time that you're trying to reference something that doesn't exist. There is never a case where this isn't true. Your GetComponent statements can be resulting in a null reference if the component isn't attached, for example, and you aren't checking those for null.