- Home /
Very Quick transform.Translate question
Why isn't this making my player model teleport to a random location on play?
using UnityEngine;
using System.Collections;
public class PlayerSpawner : MonoBehaviour {
public Transform player;
void Start () {
transform.Translate(new Vector3(Random.Range(1,100),Random.Range(1,100),-1));
}
}
Answer by robertbu · Feb 18, 2014 at 05:15 AM
I'm guessing this script is not attached to the player game object. If my guess is correct, you need to modify line 8 to:
player.transform.Translate(new Vector3(Random.Range(1,100),Random.Range(1,100),-1));
Your code is currently applying the Translate() to the game object the script is attached to, not to the player object you define at the top of the class. Note you will need to initialize the 'player' variable by dragging and dropping in the Inspector for this script to work.
You are correct thank you. I had it attatched to an empty.
Your answer
![](https://koobas.hobune.stream/wayback/20220613133903im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I spawn a random prefab? 1 Answer
Spawn object from Random Vector3 in array 0 Answers
Triggering random sound on player 1 Answer
Endless/infinite runner random enemy spawning multi lane 1 Answer
Question Local Translation PRoblem 2 Answers