Hello! I made a Script about Respawning. I can make my player respawn but I cant change the target of camera. So camera still tries to find destroyed player. Its not going to new player.
Here is the code:
void OnTriggerEnter(Collider other) //When Fall out of the map // When you enter the death barrier
{
Destroy(other.gameObject); //Destroy Player
GameObject p = GameObject.Instantiate(Player, Spawnpoint.position, Quaternion.identity) as GameObject; //Spawn new player
SmoothFollow2 sf = transform.GetComponent<SmoothFollow2>(); //Get component of camera script (I want to change target of camera)
sf.target = p.transform; // change target of the camera
}
If I understand your code correct you’re using the wrong transfrom to get the SmoothFollow2 component. I’m assuming that the SmoothFollow2 component is attached to your main camera. But I’m thinking the OnTriggerEnter method is called on some GameObject you have placed below your map. So in line 5 you’re using the transform component of this GameObject to get the SmoothFollow2 component. I’m thinking you should also see a NullReferenceExeption in the log window.
Replace line 5 with the following code to let the camera follow the new Player character:
SmoothFollow2 sf = Camera.main.GetComponent<SmoothFollow2>();
Hello! First thanks for your comment! Yes smooth follow is attached to the camera. So I changed 5th line as you said but nothing happened. Let me give you the eror code: MissingReferenceException: The object of type ‘Transform’ has been destroyed but you are still trying to access it.
put Destroy() last, it’s because you are destroying gameobject, and after that youre trying to get it’s component.
{
GameObject p = GameObject.Instantiate(Player, Spawnpoint.position, Quaternion.identity) as GameObject; //Spawn new player
SmoothFollow2 sf = transform.GetComponent<SmoothFollow2>(); //Get component of camera script (I want to change target of camera)
sf.target = p.transform; // change target of the camera
Destroy(other.gameObject); //Destroy Player
}
And also I don’t know if you have right Collision filter like
if (other.name == “Player”) {}
this way if anything enters that plane (I think this script is attached on killing plane) will trigger ERROR because if something random eg. some box falls in that plane it will check for the script and box won’t have it and lalallalallalala…