Hey guys, I'm instantiating my player in the Start() of my GameController script, and then referencing it in my Main Camera so that I can track the player's movement each frame with the camera.
BUT - When I reference the player by tag in the Start() of my camera script, it doesn't find it. I assume this is because the camera's Start() is run before the player is instantiated, so can't find it. So what should I do? Could I delay the camera so that it looks for the player AFTER it's instantiated? Should I instantiate the Main Camera too?
Here's my GameController script:
private var playerSpawn : GameObject; // a dummy object to mark where player starts
function Start () {
playerSpawn = GameObject.Find("PlayerSpawn");
var playerInstance : GameObject = Instantiate(
Resources.Load("Player", GameObject),
Vector3(playerSpawn.transform.position.x, 0, playerSpawn.transform.position.y),
playerSpawn.transform.rotation);
}
And the camera script:
public var player : GameObject;
function Start () {
player = GameObject.FindGameObjectWithTag("Player");
}
function Update () {
transform.position.x = player.transform.position.x;
transform.position.z = player.transform.position.z - 3.2;
var camerapos = transform.position - player.transform.position;
}
↧