Question by
Marinos1988 · Feb 02, 2020 at 06:37 AM ·
instantiatespawnrandom.range
Instantiate a prefab at three specific x positions
So basically i want to instantiate a prefab using Random.Range but i want it to spawn at three exact x positions for example -8, 0, 8. I am new to Unity and to coding so will need a little bit of help.
Here is what i think :
public GameObject prefab;
void Start()
{
Vector3 position1 = new Vector3(-8f, 1f, Random.Range(-20f, 70f));
Instantiate(prefab, position1, Quaternion.identity);
Vector3 position2 = new Vector3(0f, 1f, Random.Range(-20f, 70f));
Instantiate(prefab, position2, Quaternion.identity);
Vector3 position3 = new Vector3(8f, 1f, Random.Range(-20f, 70f));
Instantiate(prefab, position3, Quaternion.identity);
}
Comment
Whats the problem? This code is nice.
Just be sure the component of the vector you want is the Z and not the Y, but the code is fine. It will do what you pretend.
If is not happening, is because that prefab have some code moving the object right after instantiating it.