Quantcast
Channel: Questions in topic: "javascipt"
Viewing all articles
Browse latest Browse all 1875

Referencing instantiated player in camera's Start()

$
0
0
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; }

Viewing all articles
Browse latest Browse all 1875

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>