- Home /
Can't set camera target after instantiating a prefab(like respawn)
Hello! Help plz! Can't set camera target after I instantiate player prefab when he dies. When I start level camera works perfect, but when player dies, prefab is instantiated, but camera doesn't follow him because target is not set. I understand its typical issue but I can't find any errors in scripts. Or don't understand smth
When player dies I call RespawnPlayer()...
public class GameManager : MonoBehaviour {
public Transform player;
void Start () {
RespawnPlayer();
}
public void RespawnPlayer()
{
Instantiate(player);
var cc = GameObject.FindWithTag("MainCamera").GetComponent<CameraController>();
cc.SetTarget(GameObject.FindWithTag("Player").transform);
}
}
public class CameraController : MonoBehaviour {
public Transform target;
void Update ()
{
if (target)
{
//Move camera
}
}
public void SetTarget(Transform t)
{
target = t;
}
}
Are you really calling RespawnPlayer after it dies, your above codes shows that you are calling only at Start().
Your answer
Follow this Question
Related Questions
how to destroy camera instatiated from prefab? 0 Answers
Instantiate prefab, strange problem 1 Answer
Howto set main.camera as a parent when main.camera itself is a child of a prefab 1 Answer
Instantiating prefab camera 2 Answers
Is there an easy way to apply the same tag to all children of an object? 4 Answers