• 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 kevinspawner · Aug 10, 2015 at 08:13 AM · gameobjectinstantiate

Non Repetative Random.Range

Is it possible to randomize without repeating the same value again? As you can see in the below script, If I instantiate the game object from array, Game objects are repeating, I want the game object to be shuffled not repeated. Any help would be appreciated. Thanks in advance.

 public GameObject[] answerPrefabs;  
     public Transform[]  spawnPos;
 
     public void Start()
     {
         for(int i = 0; i<spawnPos.Length; i++)
         {
             int answerClone= Random.Range(0,answerPrefabs.Length);    
             Instantiate(answerPrefabs[answerClone],spawnPos[i].position,spawnPos[i].rotation);
         }
     }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by NeverHopeless · Aug 10, 2015 at 08:40 AM

A possibility to remove the instantiated element from array. I would suggest to use System.Collections.Generic.List<T> instead. Try like this:

  public List<GameObject> answerPrefabs;  
  public List<Transform>  spawnPos;
  
  public void Start()
  {
       for(int i = spawnPos.Length - 1; i >= 0; i--)
       {
          int answerClone= Random.Range(0, answerPrefabs.Count);    
          Instantiate(answerPrefabs[answerClone],spawnPos[i].position,spawnPos[i].rotation);

          // Remove the instantiated element from list.
          answerPrefabs.RemoveAt(answerClone);
          spawnPos.RemoveAt(answerClone);
       }
  }

Hope it helps!

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 kevinspawner · Aug 10, 2015 at 08:48 AM 0
Share

Awesome. Thanks you very much! I ave a question for you.

If I use my previous method, answerPrefabs.RemoveAt(answerClone); I cant use this as it is not a list right? So how do i remove the repetitive one?

avatar image cjdev · Aug 10, 2015 at 08:51 AM 0
Share

I think you mean Remove() ins$$anonymous$$d of RemoveAt(). The first removes the instance of the object parameter from the list while the second takes an index as a parameter and removes the object from that index in the list.

avatar image kevinspawner · Aug 10, 2015 at 10:12 AM 0
Share

Thanks. I understood now.

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

25 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

Related Questions

Cards To Hand 0 Answers

Spawn Prefabs 1 Answer

How to clone a simple GameObject where specified. 1 Answer

How to not instantiate an object if there is another object there? 1 Answer

Finding multiple objects by the same tag as to place them in one array? 2 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