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 ILoveMyDada · Apr 09, 2018 at 02:25 AM · 2dobjectsrandom.rangesetactive

How to randomize the order of objects?

Hi, so I have a situation set up where 10 different unique objects will enter from different directions and every time you press Spacebar, an object gets SetActive(false). My goal is to randomize the order successfully. And I have been able to randomize it. But the main problem is that even when lets say Object 5 gets SetActive(false), the randomization will still come back to trying to repeat Object 5 when it should not be selected anymore. Is there a better/simpler way to achieve this?

.

Any help would be appreciated!

.

 public void RandomObjectFunction()
 {
     randObject = Random.Range (1, 10);

     Objects [randObject].GetComponent<Animator> ().enabled = true;

     MasterBool = true;
 }
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

5 Replies

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

Answer by Priyanka-Rajwanshi · Apr 09, 2018 at 05:06 AM

@ILoveMyDada Try this:

 int objectsDisabled = 0;
 public void RandomObjectDisable()
 {
     if (objectsDisabled < Objects.Length)
     {
         int randObject = Random.Range(0, Objects.Length - objectsDisabled);
         GameObject selectedObject = Objects[randObject];
         Objects[randObject].SetActive(false);
         for (int i = randObject; i < Objects.Length - 1; i++)
         {
             Objects[i] = Objects[i + 1];
         }
         Objects[Objects.Length - 1] = selectedObject;
         objectsDisabled++;
     }
 }
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
avatar image ILoveMyDada · Apr 09, 2018 at 06:48 AM 0
Share

Hey so this worked a bit. It's gotten as far as making sure no objects get repeated. Only problem was that some of the objects would just deactivate. But I believe I can work with this code. It's what I was trying to get to in terms of somehow $$anonymous$$using the objects that have been used already. Thank you!

avatar image ILoveMyDada · Apr 09, 2018 at 07:09 AM 0
Share

Got it to work! I just changed Objects[randObject].SetActive(false) to Objects[randObject].GetComponent().enabled = true

Thanks so much!

avatar image
0

Answer by Cornelis-de-Jager · Apr 09, 2018 at 03:04 AM

  public void RandomObjectFunction()
  {
      // Make sure to run RandomObjectFunctionfunction only 10 times or it will hang
      while (true) {
         randObject = Random.Range (1, 10);
         if (Objects [randObject].GetComponent<Animator> ().enabled == false) {
               Objects [randObject].GetComponent<Animator> ().enabled = true;
               break;
            }
       }
      MasterBool = true;
  }
 
Comment
Add comment · Show 1 · 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 ILoveMyDada · Apr 09, 2018 at 03:19 AM 0
Share

This was close! It ends up crashing though. Is there a way around that? Thanks for the help!

avatar image
0

Answer by Chimer0s · Apr 09, 2018 at 03:08 AM

You could reassign the position of the deactivated gameobject to be the last in the array of random objects, and instead of having Random.Range(1, 10) you could replace 10 with a variable int that is deducted from each time the function is called, effectively giving you a smaller pool that no longer includes the deactivated objects.
You could also create a list of game objects and add the deactivated ones to it as you go. Then in the randomization function check if the selected object is on that list, and if so, run the function again until one not on the list is selected.

Comment
Add comment · Show 1 · 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 ILoveMyDada · Apr 09, 2018 at 03:21 AM 0
Share

Hey yeah this is something that I thought about, but I'm not that experienced enough to really come up with the code to implement it. Do you know how I could code this a bit with an example? Appreciate the response!

avatar image
0

Answer by internationalfish · Apr 09, 2018 at 06:03 AM

Make sure you have a list of references that's only used for this purpose, shuffle the list once, and just pop from it when you want an object to deactivate. No worrying about tracking used indices; pop until the list is empty.

There's good discussion on C# options for randomizing a list in this StackOverflow thread (and lots of other places, I'm sure, since this may or may not be entirely up to date). I'm not suggesting a specific one myself because it's worth reading the thread regarding performance characteristics and differing implementations.

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
avatar image ILoveMyDada · Apr 09, 2018 at 06:49 AM 0
Share

Hey thanks for the tip! Yeah I'm looking into using List ins$$anonymous$$d of Arrays since there is more functionality and freedom to mess with the objects under this setting. Cheers!

avatar image internationalfish ILoveMyDada · Apr 10, 2018 at 01:03 AM 0
Share

You're welcome. The same solution (Fisher-Yates shuffle) works with both arrays and lists, and honestly, this method would be much more efficient at solving your problem (and more readable/debuggable) than the method currently being used.

avatar image
0

Answer by NonsenseSynapse · Apr 10, 2018 at 01:15 AM

One way of simplifying some of the already proposed answers is to use the C# LINQ tool. With it, you can do something like the following:

 using System.Linq;

 var filteredList = Objects.Where(obj => obj.isActive).ToList();
 randObject = Random.Range (1, filteredList.Count);
 filteredList[randObject].GetComponent<Animator>().enabled = true;

Here are a couple resources with more information about LINQ. It's a really neat tool for filtering and sorting data, definitely worth knowing about if you're developing in C#! https://www.foreach.be/blog/java-and-net-comparing-streams-linq https://unity3d.college/2017/07/01/linq-unity-developers/

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

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

160 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Toggle 2D Object Active? 0 Answers

Spawning an object attached to one object, drawn to another. 1 Answer

Object positions in Scene view do not match Game view 1 Answer

different game "scenes" and optimization? 2 Answers

How do you make one object disappear and another appear with one button press? 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