0
\$\begingroup\$

I'm trying to instantiate different prefabs at once. But couldn't figure out how. I tried to use Random.Range method but didn't worked for me. It instantiates only one of the prefabs. I want it to instantiate all of the prefabs.

\$\endgroup\$
1
  • 3
    \$\begingroup\$ It's not clear from your question what you tried and where your mistake might be. Can you show us the code you wrote, explain what you expect it to do and what it does instead? \$\endgroup\$ – Philipp Nov 4 '20 at 11:46
-1
\$\begingroup\$

You haven't explained nearly enough about what you're trying to do, but here are some examples:

public List<GameObject> prefabs;

//Instantiate one of each prefab
public void InstantiateAll() {
    foreach (var prefab in prefabs) {
        Instantiate(prefab);
    }
}

//Create the given number of instances from random prefabs
public void InstantiateRandom(int count) {
    for (int i = 0; i < count; i++) {
        int index = UnityEngine.Random.Range(0, prefabs.Count);
        var prefab = prefabs[index];
        Instantiate(prefab);
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ Please don't answer questions when they don't explain enough about what they are trying to do. Convince the author to clarify their question through an edit and then answer the question when you actually know what help they need. \$\endgroup\$ – Philipp Nov 4 '20 at 18:25
  • \$\begingroup\$ @Philipp That's hardly worth a downvote over. I addressed the two things he mentioned in his post, "instantiate all of the prefabs" and "use Random.Range". A more thorough question is definitely better, but these are easy answers that I would expect to mostly or completely solve whatever simple problem he's having. \$\endgroup\$ – Kevin Nov 4 '20 at 19:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.