Spawning random objects not working
Hello everyone. So i managed to spawn random obstacles. But they dont respawn at a random position they always respawn on the same position. Why is that?
Code:
using UnityEngine;
using System.Collections;
public class Spawner : MonoBehaviour {
public GameObject[] prefabs;
public float delay = 2.0f;
public bool active = true;
public Vector2 delayRange = new Vector2 (1, 2);
// Use this for initialization
void Start ()
{
ResetDelay ();
StartCoroutine (ObstacleGenerator ());
}
IEnumerator ObstacleGenerator(){
yield return new WaitForSeconds (delay);
if (active)
{
var newTransform = transform;
Instantiate(prefabs[Random.Range(0, prefabs.Length)], newTransform.position, Quaternion.identity);
ResetDelay ();
}
StartCoroutine (ObstacleGenerator ());
}
void ResetDelay(){
delay = Random.Range (delayRange.x, delayRange.y);
}
}
Thank you
Comment
Answer by etopsirhc · Oct 07, 2015 at 12:17 AM
you are setting newTransform to equal the script's gameobject's transform every time. instead try setting the location around where you want them to spawn, then adding a random amount to that.
Answer by Shkizi · Oct 07, 2015 at 12:20 AM
I tryed like this:
Instantiate(prefabs[Random.Range(0, prefabs.Length)], new Vector2(Random.value, Random.value), Quaternion.identity);
Still not random position. They always spawn at the same position
do any of the scripts you use set the random seed? and try multiplying the value of the random, as it only gives 0f-1f (Random.value*20 for a test)