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 /
  • Help Room /
avatar image
0
Question by Joorst · Aug 31, 2016 at 10:22 PM · c#listrandomactivateobjectpool

How to Activate Random GameObjects from a list without affecting position

Hi there, I am trying to activate a random GameObject from a list, but anything I try also activates the GameObject at a random position for a random amount of time before it is deactivated.

Is there any way to activate a random GameObject from a list without affecting the position?

This is what my original code looks like before trying to randomize activation.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class poollist : MonoBehaviour {
 
     public float fireTime = 2f;
     public GameObject Log1;
     public GameObject Log2;
     public GameObject Log3;
     public GameObject Log4;
 
     public int pooledAmount = 10;
     List<GameObject> Logs= new List<GameObject> ();
 
 
     // Use this for initialization
     void Start () {
         
         for (int i = 0; i < pooledAmount; i++) {
             GameObject obj1 = (GameObject)Instantiate (Log1);
             GameObject obj2 = (GameObject)Instantiate (Log2);
             GameObject obj3 = (GameObject)Instantiate (Log3);
             GameObject obj4 = (GameObject)Instantiate (Log4);
             obj1.SetActive (false);
             obj2.SetActive (false);
             obj3.SetActive (false);
             obj4.SetActive (false);
             Logs.Add (obj1);
             Logs.Add (obj2);
             Logs.Add (obj3);
             Logs.Add (obj4);
         
         }
         InvokeRepeating ("Fire", fireTime,fireTime);
     
     }
 
 
 
     void Fire ()
     {
 
         for (int i = 0; i < Logs.Count; i++) {
             if (!Logs [i].activeInHierarchy) {
                 Logs [i].transform.position = transform.position;
                 Logs [i].transform.rotation = transform.rotation;
                 Logs [i].SetActive (true);
                 break;
             }
         }
     }
 }

I've tried changing Logs [i].SetActive (true); to Logs [Random.Range(0, Logs.Count)].SetActive (true);, but it also activates the GameObject at a random position.

Yes I am new to coding, still finding my way around, and I've been sitting on this for too long. Any help will be greatly appreciated, if my question or code is too vague I'd be happy to clear it up. Thanks.

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
0
Best Answer

Answer by ToothPaste17 · Sep 01, 2016 at 08:58 AM

I think the problem is that you're setting the position and rotation of one gameobject, but activating a different one. Try changing your Fire method to this:

 void Fire(){
         int randomIndex = Random.Range(0, Logs.Count);
         Logs[randomIndex].transform.position = transform.position;
         Logs[randomIndex].transform.rotation = transform.rotation;
         Logs[randomIndex].SetActive(true);
         Logs.RemoveAt(randomIndex);
     }

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 Joorst · Sep 01, 2016 at 09:28 AM 0
Share

Hi there ToothPaste17, thanks for your reply. It does work, thank you so much. I am though having a different issue now. depending how big I make the pool, after a few seconds it gives me an "Argument out of range" error.

avatar image ToothPaste17 Joorst · Sep 01, 2016 at 10:35 AM 0
Share

The method removes one item from the list each time it is called, until the list is empty eventually. But the method keeps trying to pull items from the pool even though it is empty, which causes the error. The solution is to check if the list is empty, and if so, either refill it or cancel the invoke. For example:

 void Fire()
 {
          if(Logs.Count == 0) 
          {
               CancelInvoke();
               return;
          }
          int randomIndex = Random.Range(0, Logs.Count);
          Logs[randomIndex].transform.position = transform.position;
          Logs[randomIndex].transform.rotation = transform.rotation;
          Logs[randomIndex].SetActive(true);
          Logs.RemoveAt(randomIndex);
  }

avatar image Joorst ToothPaste17 · Sep 01, 2016 at 06:27 PM 0
Share

Thanks, I would have liked to mark your answer as accepted since its exactly what I've been looking for, but it's a comment. One last thing though, I've tried finding out for myself how and where to refill the list after it's emptied out, but I just can't come up with anything, I think I might be trying to over analyze it. For it's intended purpose the code, as it is, would work fine, I don't need to refill the list, but would you $$anonymous$$d helping me explaining how to go about refilling it so I could use it in future as well please?

Thanks again!

Show more comments

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

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

What are the steps for the player to be able to create a list of items, then have the computer select and show a single item from that list? 1 Answer

What range of values need to be inserted if we need to pull a random item from the list? 0 Answers

How do I get random answers? 2 Answers

How would I implement a random spawn timer into a list based spawn system? 2 Answers

How Can I Download Multiple Files With Unity Web Request? 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