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
1
Question by Giova507 · May 18, 2014 at 09:52 PM · c#randomactivatehow-toobjectpool

How to Activate Random GameObjects? (C#)

I'm using objectpool to instantiate all my prefabs at the start of the game, how do I activate them randomly every 10 seconds?

Example: I have 5 Colors, every 10 seconds 1 Color Shows Up and the last one goes away. The five colors are instantiated but deactivated. I want to activate them Randomly.

Comment
Add comment · Show 1
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 robertbu · May 18, 2014 at 10:22 PM 0
Share

Seeing your script would help get an accurate answer. See Random.Range() for selecting a new random color.

1 Reply

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

Answer by Chris_Dlala · May 19, 2014 at 05:29 PM

Without a look at the script I'm not sure how much this will help but I'll have a go. If your pool is an array/list of GameObjects you could use Random.Range() to find a random element: objectPool[Random.Range(0, objectPool.Length)]. If you only want one active object at a time you could then store a reference/index for the current active object so that you can deactivate the current and activate a new GameObject:

     public GameObject[] objectPool;
     private int currentIndex = 0;
 
     public void NewRandomObject()
     {
         int newIndex = Random.Range(0, objectPool.Length);
         // Deactivate old gameobject
         objectPool[currentIndex].SetActive(false);
         // Activate new gameobject
         currentIndex = newIndex;
         objectPool[currentIndex].SetActive(true);
     }

Now to make this happen every 10 seconds you could monitor time yourself in an update, use a Coroutine or you can use Monobehaviour's InvokeRepeating. Here's a super quick example of a timer in update:

     float elapsedTime = 0f; // Counts up to repeatTime
     float repeatTime = 10f; // Time taken to repeat in seconds
 
     private void Update()
     {
         elapsedTime += Time.deltaTime;
         if (elapsedTime >= repeatTime)
         {
             // Do something here
             ...
             // Subtract repeat time
             elapsedTime -= repeatTime;
         }
     }


I hope some of that was useful. =)

Comment
Add comment · Show 4 · 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 Giova507 · May 19, 2014 at 07:53 PM 0
Share

Thank You! This is what I was looking for! :D

avatar image 20Jay99San04 · May 08, 2016 at 02:55 PM 0
Share

Thanks for this good answer.

avatar image richener · Jan 19, 2017 at 02:21 PM 0
Share

Thanks for this code :) i use this for my random panel and how about random panel without repeating?

avatar image Cryptonyte richener · Sep 07, 2018 at 01:13 AM 0
Share

Every time a new number is used, add it to another list/array and have your initial random code to check if that number is in your other list/array. If not, choose again.

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

26 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Spawn Random Enemy 3 Answers

instantiating vertically 2 Answers

Selecting random indexes without repetition 3 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