- Home /
unity chose random gameobject and spawn it.
So I haven't got a code for it yet but I have been struggling for a while trying to figure out how to spawn random gameobject out of about 10 at the location of a different gameobject of which the script is on. So every 5 seconds for example on of the gameobects will spawn on the gameobject with the script.
Answer by bpaynom · Jul 09, 2019 at 06:24 PM
So you have object A that has a script that spawns another object (1 of 10 randomly) at A's location. ? If that's the case:
public Spawner : MonoBehaviour {
[SerializeField] private GameObject[] objectsToSpawn;
void Start()
{
Spawn();
}
public GameObject Spawn()
{
int index = objectsToSpawn.Length;
if ( index > 0 )
{
index = Random.Range(0, index);
return Instantiate ( objectsToSpawn[ index ] , transform.position, Quaternion.identity );
}
return null;
}
}
If you want random rotation, you can change Quaternion.identity
for something like Quaternion.Lerp( rotationA , rotationB, Random.Range(0f,1f) );
, or you can set it as Quaternion.identity
and then rotate the instantiated object Random degrees.
I like this! The only thing I would add is you would need to attach this script to the transform that you want to spawn at, or you could tweak a bit with this, just create an empty game object and then attach drag it into the spawnPosition slot made in this script:
public Spawner : $$anonymous$$onoBehaviour
{
[SerializeField] private GameObject[] objectsToSpawn;
public Transform spawnPosition;
void Start()
{
Spawn();
}
public GameObject Spawn()
{
int index = objectsToSpawn.Length;
if ( index > 0 )
{
index = Random.Range(0, index);
return Instantiate ( objectsToSpawn[ index ] , spawnPosition.position, Quaternion.identity );
}
return null;
}
}
@ConcanoPayno Ok so I now have an object spawning every one second. How would I give it a random range of rotations eg. transform.rotation(0, 0, Random.Range(22, -22),
Now it is starting to get a bit more convoluted and requires more of an understanding of the fundamentals of coding in general; also I'm at work and don't have an IDE that can compile this code, but it should more or less give you what you are asking for:
public Spawner : $$anonymous$$onoBehaviour
{
[SerializeField] private GameObject[] objectsToSpawn;
public Transform spawnPosition;
public float timeBetweenSpawns;
bool isSpawning;
void Update()
{
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)
{
isSpawning = true;
}
float countDown = timeBetweenSpawns
if(isSpawning == true)
{
int index = objectsToSpawn.Length;
if ( index > 0 )
{
index = Random.Range(0, index);
countDown -= time.deltaTime;
if (countDown <= 0)
{
Instantiate ( objectsToSpawn[ index ] , spawnPosition.position, Quaternion.identity );
countDown = timeBetweenSpawns
}
if(index < 0)
{
isSpawning = false;
}
}
}
There are other ways with coroutines and having a much cleaner update function that would check when to start spawning and stop spawning, but writing the functions in a coroutine, update, or fixedUpdate are the only ways to control time.
He talks about random rotation. He said he already has it working every second. I updated my answer introducing the rotation thing.
Answer by RlcZyro · Jul 09, 2019 at 08:08 PM
Thanks man your scripts really helped and with a bit of tweaking I got it working :)