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.
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.
– Grimshad