I instantiate a cube (prefab tagged "Respawn") at random locations within a grid. I need my First Person Player Controller to spawn directly on top of it.
Here's what I have attached to my FPS Controller:
var startCube;
function Start ()
{
startCube = GameObject.FindWithTag("Respawn");
transform.position = startCube.transform.position;
}
function Update ()
{
print(transform.position); //to see if the position changes
}
Every time I run my game the FPS controller spawns in the same position.
What am I doing wrong?
You don't include the code where you randomly spawn your cubes and the problem might be there, but I'll try my best with what you posted.
Your script calls GameObject.FindWithTag("Respawn") and the fact that it returns a GameObject (you don't mention an error on the next line where you try to access the transform meaning it found one) means that there is an instance of some gameObject with the tag "Respawn" in your scene and that is where your transform in the posted script is being moved to. To prevent this, either remove the extra object tagged "Respawn" or get all gameObjects tagged "Respawn" and then pick the one you want from there somehow.
Also, are your cubes instantiated in or before Start in your posted script? If not, then your cubes won't exist and you'll have another problem.
On a side note, you needn't print the position in Update. If you print in start, it will keep your log cleaner.