Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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.

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

Affect every object in array. 1 Answer

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

Random instantiation endlessly? 1 Answer

Problem not remove all prefab from list aftre the instantiate 1 Answer


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