- Home /
Null Reference exception help
Would someone please help me find this Null Reference Exception?
NullReferenceException: Object reference not set to an instance of an object RespawnDieTrigger.OnTriggerEnter (UnityEngine.Collider hit) (at Assets/Troys Scripts/RespawnDieTrigger.js:17)
RespawnDieTrigger is the trigger script which is attached to the main game object(named Sphere) which is the following script. Below this script is my Respawn script which is attached to the first person controller (named FPS)
tum1-2 is the static trigger object(its a wall)
When the sphere hits the wall(tum1-2) the First Person Controller and all of its children(main camera and sphere) respawn perfectly. In my Hierarchy each time the sphere hits the wall(tum1-2) I see the FPS, FPS(Clone) FPS(Clone)(Clone)etc.. The (Clone) just adds on to the end. So what I see in the Hierarchy after five collisions is FPS(Clone)(Clone)Clone)(Clone)(Clone).Just one FirstPerson Controller with five strings of (Clone) after it. Any Ideas why I'm getting this Null Reference Exception?
function OnTriggerEnter(hit:Collider){
if (hit.name == "tum1-2"){
// if player hit the wall...
Destroy(gameObject.Find("Main Camera"));
Destroy(gameObject);
Destroy(gameObject.FindWithTag("GameController"));
// it suicides
// get the object tagged "Respawn"
var objRespawn = GameObject.FindWithTag("Respawn");
// get the script RespawnScript.js
var script: RespawnScript = objRespawn.GetComponent(RespawnScript);
script.RespawnPlayer(); // respawn a new player
}
}
`
`
var playerPrefab: GameObject; // drag the player prefab here
function RespawnPlayer(){
Instantiate(playerPrefab, transform.position, transform.rotation);
}
For a start, gameObject
and GameObject
are not the same thing. Secondly, which is line 17?
Answer by Statement · Aug 10, 2011 at 04:15 PM
function OnTriggerEnter(hit : Collider) {
if (hit.name == "tum1-2") {
// This will cause null reference exception if you
// don't create a new "Main Camera" object somewhere
Destroy(GameObject.Find("Main Camera"));
// This will cause null reference exception if you
//don't create a new object with "GameController" tag somewhere
Destroy(GameObject.FindWithTag("GameController"));
Destroy(gameObject);
var objRespawn : GameObject = GameObject.FindWithTag("Respawn");
// This will cause null reference exception if you
// have no game object with the "Respawn" tag somewhere
var script : RespawnScript = objRespawn.GetComponent(RespawnScript);
// This will cause null reference exception if you
// have no "RespawnScript" script on the object with the "Respawn" tag.
script.RespawnPlayer();
}
}
Since you're getting names like "FPS(Clone)(Clone)Clone)(Clone)(Clone)", I start to think that your player prefab isn't actually a prefab, but an object in your scene. The idea is that you should create a prefab in your project and link the prefab to playerPrefab, not the actual player placed in the scene.
var playerPrefab : GameObject;
function RespawnPlayer() {
// This will cause null reference exception if you
// a) Forgot to set playerPrefab to a valid reference or,
// b) Have destroyed playerPrefab, like in the case if you
// linked to a scene object or,
// c) Set it to null elsewhere in your code.
Instantiate(playerPrefab, transform.position, transform.rotation);
}
Note: I changed gameObject.Find etc to GameObject.Find as well. The function is static, not a member.
Answer by fredpointzero · Aug 10, 2011 at 02:10 PM
There are several places where you can have a null reference : if hit is null => "if(hit.name == "tum1-2")"
if "Respawn" is not found, objRespawn will be null and then "objRespawn.GetComponent" will throw an error
if "RespawnScript" is not found => script.RespawnPlayer will throw an error
And if I remember well, GameObject.Destroy does not accept null as argument, so if you use a "Find" method that return null, it could throw a null reference.
Hope it helps !
thank you. I think its line 17 - script.RespawnPlayer() but I don't know why its not being found except that I've destroyed it. If this is the reason what do you suggest?
"but I don't know why its not being found except that I've destroyed it", sorry but that made me laugh. If you destroyed it, you can't find it now can you? :) It's gone. Poof. What about you destroy it after you cloned it then? But seriously, you should try making it a prefab ins$$anonymous$$d. It's getting obvious that you've linked in a scene object.