- Home /
null reference exception when setting cam target?
hey,
'm fairly new to scripting in C# and have recently started to create a 2D platfromer after watching a few tutorials. I have have the controls, physics and camera following my player, but when attempting to implement a respawn I have a problem resetting the camera target. When the player object enters the death zone the script calls a die function that destroys the player then instantiates a new one.
whenever I try to set the camera target I get the error null reference exception any and can't seem to get around it. Any Ideas the code where the error occurs is below.
any Ideas the code where the error occurs is below.
//actions to be excuted when die is called
public void Die(){
Debug.Log("Destroy Player");
//create new player
player = (GameObject) Instantiate(player,Vector3.zero,Quaternion.identity);
//new player as camera target
cam.SetTarget(player.transform);
//destroy current player
Destroy(gameObject);//kill current player
Debug.Log("lose life");
} ##
You've only included part of your code, and a paste of the error from the console window is often helpful for these kinds of errors. I'm going to guess that the error is on line 11. Where does 'cam' get declared and initialized? SetTarget() is not a Camera function, so this has to be some script you've created or borrowed.
Yeah the error does occur on line 11 or wherever I place that line of code.
the error message I get is:
NullReferenceException: Object reference not set to an instance of an object PlayerControls.Die () (at Assets/Scripts/PlayerControls.cs:142) DeathScript.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/DeathScript.cs:24)
What that tells you is that 'cam' has not been initialized. You've not included enough code for use to advise you on what to do to fix the problem. We need to see where the variable is declared and any code that tries to initialize the variable.
In the player controls scrip the cam varible is only used in start and the die function
public GameObject player;
//////////////////////////////////////////////////////////////
private gameCamera cam;
////////////////////////////////////////////////////////
// Use this for initialization
void Start () {
cam = GetComponent<gameCamera>();
}
/////////////////////////////////////////////////////////////////////////////////////// }
//actions to be excuted when die is called
public void Die(){
Debug.Log("Destroy Player");
//create new player
player = (GameObject) Instantiate(player,Vector3.zero,Quaternion.identity);
//new player as camera target
//destroy current player
Destroy(gameObject);//kill current player
Debug.Log("lose life");
cam.SetTarget(player.transform);
}
}
The variable is also used in a game manager script.
public class game$$anonymous$$anager : $$anonymous$$onoBehaviour {
public GameObject player;
private gameCamera cam;//create camera object
// Use this for initialization
void Start () {
// get reference to to the game camera.
cam = GetComponent<gameCamera>();//get acces to gamecamera script
//spawn player
SpawnPlayer();//execute spawn function
}
/////////////////////////////////////////////////////////////////////////////
// Spawn player
public void SpawnPlayer() {
//set camera target as player
cam.SetTarget((Instantiate(player,Vector3.zero,Quaternion.identity) as GameObject).transform);
}
}
Your answer
Follow this Question
Related Questions
Line problem 0 Answers
Player camera targeting/locking another object 0 Answers
Camera gets stuck when cursor is locked 0 Answers
How to make the second camera rotate automatically? 1 Answer