• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by munggol97 · Jun 04, 2019 at 01:46 PM · instantiatelistfor-loopspawning problemsargumentoutofrangeexception

Spawn an object to a random spawn point from a list

I want to spawn 3 of the same game object at a random spawn point that I put into a list. But everytime I run my project there's an error saying ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

I was wandering how does this error is happening, or there can be a better way to do what i am trying to implement?

 private void Start()
 {
     foreach (GameObject obj in GameObject.FindGameObjectsWithTag("Savee"))
     {
         saveePoints.Add(obj);
     }
     spawn();
 }

 void spawn()
 {
     for (int i = 0; i <= 2; i++)
     {
         i = Random.Range(0, saveePoints.Count);
         saveePoints[i] = Instantiate(saveeObject, saveePoints[i].transform.position, transform.rotation) as GameObject;
     }
 }
  




Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Metais · Jun 04, 2019 at 01:57 PM

You are changing the value of i inside your for loop, which you are already using to cycle through it three times. Change the i to j or similar, perhaps that helps?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by SirPaddow · Jun 04, 2019 at 02:03 PM

You shouldn't use i to count your instances and to get a random spawn position. Try something like this.

  void spawn()
  {
      for (int i = 0; i <= 2; i++)
      {
          int spawnIndex = Random.Range(0, saveePoints.Count);
          Instantiate(saveeObject, saveePoints[spawnIndex].transform.position, transform.rotation);
      }
  }

If the error is still showing after, make sure your Start function actually finds the spawn positions.

Also, I don't understand why you replace your spawn positions with your new instances... Can you tell what you are trying to do exactly?

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image munggol97 · Jun 04, 2019 at 02:31 PM 0
Share

Thanks for your feedback I still got the error but as you say my start function is not founding my spawn points I noticed it by puting a Debug.Log inside the foreach loop. What should I do? $$anonymous$$aybe it is because my spawn points were also instantiated when the game starts.

avatar image Metais munggol97 · Jun 04, 2019 at 02:56 PM 0
Share

You could try to instantiate the list of save points as a new, empty list before adding new objects to it, that way you are certain of the size of the list and can understand your indexoutofbounds exception better.

Like this:

  private void Start()
  {
      saveePoints = new List<GameObject>();
      foreach (GameObject obj in GameObject.FindGameObjectsWithTag("Savee"))
      {
          saveePoints.Add(obj);
      }
      spawn();
  }

avatar image SirPaddow munggol97 · Jun 04, 2019 at 04:05 PM 0
Share

Does your scene actually contain gameobjects with the tag "Savee"? I can't see why the find function would fail, but you can try to avoid it by assigning your spawn positions through the inspector by making "saveePoints" public.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

134 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

ArgumentOutOfRangeException: Argument is out of range in Generic.List 2 Answers

How do I Instantiate a prefab in a list? 0 Answers

i need Spawn Object loop if find object tag ("Clone") in list ,when i delete find object tag ,cript is good but no find object, please help me 1 Answer

Object reference not set to an instance of an object with object set, 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges