Angelsm
Angelsm

Reputation: 339

Spawning several random objects in several random positions only when a scene begins

I want spawning several random objects in several random positions only when a scene begins. How can I do it? This is my code, but only appears one object.

using UnityEngine;
using System.Collections;

public class SpawnItems : MonoBehaviour
{
    public Transform[] SpawnPoints;
    public GameObject[] Objetos;

    void Start () 
    {
        int spawnindex = Random.Range (0, SpawnPoints.Length);
        int objectindex = Random.Range (0, Objetos.Length);

        Instantiate (Objetos [objectindex], SpawnPoints [spawnindex].position, SpawnPoints [spawnindex].rotation);
    }

}

Upvotes: 0

Views: 5753

Answers (3)

DisturbedNeo
DisturbedNeo

Reputation: 743

I made a function to do exactly this for a game I'm working on.

public List<GameObject> spawnPositions;
public List<GameObject> spawnObjects;   

void Start()
{
    SpawnObjects();
}

void SpawnObjects()
{
    foreach(GameObject spawnPosition in spawnPositions)
    {
        int selection = Random.Range(0, spawnObjects.Count);
        Instantiate(spawnObjects[selection], spawnPosition.transform.position, spawnPosition.transform.rotation);
    }
}

The way it works is you place your different locations in a list, and all of the different prefabs of objects you want to spawn in a separate list, then the loop spawns a random object at each of the different positions.

Remember to put:

using System.Collections.Generic;

At the top of the class in order to use Lists. You could also use Arrays for this, but I personally prefer lists.

And the Start() function is only ever called once per object, so this code will only run once each time the scene is loaded.

Upvotes: 0

Kasta
Kasta

Reputation: 1994

You can do it just with loop

    Start()
    {
         int numberOfObjectsToSpawn = 10;
         int spawnindex;
         int objectindex

         for(int i = 0; i < numberOfObjectsToSpawn; i++){

         spawnindex = Random.Range (0, SpawnPoints.Length);
         objectindex = Random.Range (0, Objetos.Length);


         Instantiate (Objetos [objectindex], SpawnPoints [spawnindex].position, SpawnPoints [spawnindex].rotation);

         }
    }

Hope I get your question right.

Upvotes: 1

Tom
Tom

Reputation: 2472

I believe this is because you are calling spawnindex in the Start() function.

This means that your spawnindex will always be the same number as the Start() function more or less only happens once when you hit play mode.

This also means objectIndex has the same number, so it will always spawn the same object.

This means that all your spawned objects are using the same number to spawn. If you look at your hierarchy your GameObjects are probably all in the same position and the same object.

You need to find a way to generate a different number for each GameObject. :-)

EDIT:

This code is from one of the tutorials from the Unity website. It spawns objects at a set time rate. You could do the same but have the spawn time rate at a very small number, that way all your GameObject's will appear to have spawned at the same time.

void Start ()
    {
        // Call the Spawn function after a delay of the spawnTime and then continue to call after the same amount of time.
        InvokeRepeating ("Spawn", spawnTime, spawnTime);
    }


    void Spawn ()
    {
        // If the player has no health left...
        if(playerHealth.currentHealth <= 0f)
        {
            // ... exit the function.
            return;
        }

        // Find a random index between zero and one less than the number of spawn points.
        int spawnPointIndex = Random.Range (0, spawnPoints.Length);

        // Create an instance of the enemy prefab at the randomly selected spawn point's position and rotation.
        Instantiate (enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
    }

Upvotes: 0

Related Questions