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 Vauxvogh · Jul 11, 2019 at 04:45 AM · uibuttonrandomsetactive

How can I set active a random Ui object from a list on a button press then reset it after the button pressed?

How can I set a random ui object active from a list of ui objects on button click then reset the list so that if i click again it will activate another random ui object? So basically im making a chest reward on the ui and the player can click on the chest image to open it then I set active a random ui object from a list and that rewards them. But after that if the player clicks the chest button again the same ui object pops back up because its already set active. I want it to where if the player clicks the chest button again i need another random ui object from the list to activate. Thanks for the help

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 xxmariofer · Jul 11, 2019 at 06:27 AM

 private List<GameObject> activeUIobjects = new List<GameObject>();//this is just in case you want to desactivate it
 private List<GameObject> inactiveUIobjects = new List<GameObject>();//fill your list 
 
 public void ActivateRandom()
 {
    int random = Random.Range(0, inactiveUIobjects.Count);
    
    inactiveUIobjects[random].SetActive(true);
    
    activeUIobjects.add(inactiveUIobjects[random]);
    inactiveUIobjects.RemoveAt(random);
 }

Not tested but should work

Comment
Add comment · Show 6 · 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 Vauxvogh · Jul 11, 2019 at 06:35 AM 0
Share

Hmm I'm getting red squiggles in the

int random = Random.Range(0, inactiveUIobjects); (the inactiveUiobjects part)

and then on the add part of the activeUiobjects.add

also would i attach this to a ui elememt for the list then just add this to a button click event?

avatar image Vauxvogh · Jul 11, 2019 at 06:41 AM 0
Share

Ok the first part is fixed with the ".Count" added

but the .add part still gets the error nope fixed it sorry just capitalize the Add totally forgot facepalm

avatar image Vauxvogh · Jul 11, 2019 at 07:12 AM 0
Share

Hmm thanks a ton for the answer. It seems to be working but not quite here is how it goes I attached the script to game object changed the private list on inactiveuiobjects to public so i could add the ui objects to list from the editor. then I take the chest Button and drag to gameobject with the script attached to it to the on click button event and set it to "ActivateRandom()"

then when I click the button it works it set actives one ui object from the list, But when I close the ui object as in press its button attached to it to set active to false. The Chest button cant activate it again. So basically if its been chosen from the list already It cant be activated again after ive already deactivated it witht the other button

avatar image Vauxvogh · Jul 11, 2019 at 07:26 AM 0
Share

Alrighty Final Comment haha, I figured it out exactly what I wanted it to do. If you don't want it removed at all and would like it to keep activating random form the list then just remove the line "inactiveUIobjects.RemoveAt(random);

Awesome. Thanks a ton man

avatar image xxmariofer Vauxvogh · Jul 11, 2019 at 07:29 AM 0
Share

i thought you wanted to only desactivate it once my fault, and sorry for the mistakes i code it in the answer without the inteligence

avatar image Vauxvogh xxmariofer · Jul 11, 2019 at 08:05 AM 0
Share

No no it's perfect! Thanks so much for taking time to answer. It works just fine and taught me alot!

Cheers :)

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

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

Script Two Button Functions At Once 1 Answer

NullReferenceExeption on gameObject.SetActive 1 Answer

one button to hide and unihed gameobjects 0 Answers

how to genrate matching numbers on ui buttons in an 4*4 matrix ??? 0 Answers

How to hide non-active button? 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