C# Player Respawning HELP

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 
    }

bump

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>();

Hope this helps.

What in particular isn’t working? Is the collision event being called at all? Are you seeing anything in your log console? Warnings or errors?

If you add a Debug.Log() call to your OnTriggerEnter function, do you see that in the log? Something like this:

void OnTriggerEnter(Collider other)
{
	Debug.Log("OnTriggerEnter: " + other.gameObject, other.gameObject);
	//...rest of your function goes here
}

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…

Thanks for the reply but still cant find any solution :confused: