Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Basti77 · Mar 26, 2015 at 11:33 PM · gameobjectarraylistarraylist

GameObject to Array/List or whatever fits best

Hello,

I have the following problem, i'am struggling with it and i really getting frustrated about it. So that's why i ask this question (i really hope its not asked to often, i did look for answers on this forum but didn't find anything that i could use).

So this what i want to do: I have 20 game objects with a Tag called answer. I want to select max 5 (unique) answers (game objects) from the total of 20 answers. I had the following code and a lot of variations of it, but i keep getting errors.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class RandomAnswer : MonoBehaviour {
 
 public GameObject[] AnswerList;
 public GameObject[] NewAnswerList;
 public int MaxAnswers = 5;
 
 void Start()
     {
         AnswerList = GameObject.FindGameObjectsWithTag("answer");
     }
 
     void AnswerListCheck ()
     {
         while (AnswerList.Length < MaxAnswers)
         {
             int i = Random.Range(0, AnswerList.Length);            
             AnswerList.Remove(i);
         }
     }
 }


I really hope guys can help me out.

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 AlwaysSunny · Mar 26, 2015 at 11:03 PM

Unless it's totally inconvenient, it's better to keep a collection of objects than to "find" them. I use zero "finds" in my project, because I feel like they're messy. Expose a public list or array of gameobjects and populate the list in the inspector.

 public List<GameObject> allObjects;

If your objects have an Answer script, you could keep a collection of those instead for more direct access to the properties you need. The collection should be of the type you will make use of most frequently. If that's GameObject, then a list or array of GameObjects is fine.

To satisfy your unique randoms requirement, keep a collection of those answers you have selected, and reject any random selection which is already represented in the collection:

 List<GameObject> selected = new List<GameObject>();
 while ( selected.Count < 5 ) {
   int randomIndex = Random.Range( 0, allObjects.Count );
   if ( !selected.Contains( allObjects[randomIndex] ) )
     selected.Add( allObjects[randomIndex] );
 }

As long as your collection contains at least as many objects as you want (five or more) this code will safely run and terminate.

Comment
Add comment · Show 2 · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

What is the best way to instatiate an array of connected GameObjects? 0 Answers

How to select an Game Object from an Item List 0 Answers

Problems creating new List of type GameObject and adding GameObject to it. 1 Answer

How can you do calculations on two lists? 1 Answer

How to drag and drop any object to any empty space? 0 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