Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 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 /
  • Help Room /
avatar image
0
Question by adhamyasser · Dec 01, 2015 at 05:35 PM · animationarrayrandomsetactive

randomly activate one of many gameobjects ??

I gave 18 gameobjects that I want too set to active through script. the problem is that it will be inefficient to write a script and set each one o the 18 gameobjects to true or false. so is there a way to do this using an array and randomly set them to active ??

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

4 Replies

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

Answer by servival · Dec 01, 2015 at 06:22 PM

This is so easy.

 public GameObject[] ObjectsList; // Not only 18, this script will except any number of game objects you put here.
     
     void Start() {
     ObjectsList[Random.Range(0,ObjectsList.Length)].SetActive(true);
     }
 

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 Jessespike · Dec 01, 2015 at 06:28 PM 0
Share

Executing that code multiple times might try to activate objects that are already active.

avatar image servival Jessespike · Dec 01, 2015 at 06:35 PM 0
Share

I gave him the base code and he can start from it.

avatar image imLor · Sep 17, 2021 at 05:55 PM 0
Share

Hello! And how to do the same, but for the random inclusion of scripts?

avatar image
3

Answer by Jessespike · Dec 01, 2015 at 06:45 PM

 using System.Linq;  // Top of the script, outside of the class


 public GameObject[] arrayOfGameObjects;
 
 // Randomly activates an inactive game object
 public void ActivateRandomObject()
 {
     GameObject selection = arrayOfGameObjects
     .Where(i=>!i.activeSelf)
     .OrderBy(n=>Random.value).FirstOrDefault();
 
     // selection will be null if all game objects are already active
     if (selection != null) selection.SetActive(true);
 }

Thanks goes to dubbreak at Select random object in list using linq

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 imLor · Sep 17, 2021 at 05:56 PM 0
Share

Hello! And you do not know how to do the same, but for the random inclusion of scripts at startup?

avatar image
0

Answer by xx_boy_nichi · Aug 24, 2018 at 07:23 AM

Tnx man :D

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 Paulo-Melo · Nov 26, 2019 at 08:50 PM

Thank you very much ! It worked for me to set active ramdom gameobjects :)

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

53 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

Related Questions

Enemy has 2 special killing animation, I want to call those randomly when player's health low 0 Answers

C# Random String from List 2 Answers

How to set proper animation to prefabs randomly instantiated? 1 Answer

C# Adding random values to array 1 Answer

Animations On Random isn't working 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