• 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
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 t$$anonymous$$s 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

People who like this

0 Show 0
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

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

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

Comment

People who like this

0 Show 0 · 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

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 somet$$anonymous$$ng like t$$anonymous$$s.

  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

People who like this

0 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? Maybe 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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Creating text at the position of the gameobjects in world 0 Answers

Using event system to respawn dead enemies will spawn live ones again too 0 Answers

How can I continue to instantiate an object after deleting 0 Answers

How to stop instantiating a certain object / objects from a list . 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges