- Home /
Camera cannot find instantiated player
Hi guys, I have a problem, my camera won't follow instantiated player on spawn position it always send me an error: The object of type Transform has been destroyed but you are still trying to access it! This is the code which I'm using:
void OnTriggerEnter(Collider other)
{
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
}
can you tell me what line number in your code that make the error happens?
It's not error in the code, error is that camera doni't follow instantiated player but it goes on the first position and trying to access to the first one player which is destroyed when you lose or fall of. And I don't know what I'm doing wrong because it's seems to me that code is ok
what about to just change your player position (optionally rotation too) ins$$anonymous$$d of destroying and instantiating prefab?
Answer by alok-kr-029 · Dec 23, 2014 at 10:22 AM
1.Before Destroying the gameobject disable the script that camera is accessing to handle the gameobject 2. then Instantiate the player and find the gameobject by Gameobject.Find("Player(Clone)"); 3.then enable the camerascript and assign the player gameobject to the script
Hope this will help