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 dakshesh1010 · Dec 31, 2015 at 01:38 PM · arraysgameobjectsendless runnerrandomspawning

SetActive() not working on pooled objects?

I have a Pool of objects (cars) from which I want to spawn objects at some random intervals. So to choose a random object from the pool, I have a function which chooses a random object, and tries to activate it using SetActive() function. Here is the function.

 void ChooseCarToActivate()
     {
         int randomArg = (int)Random.Range (0, laneCars.Length);
         if (!laneCars [randomArg].activeInHierarchy) 
         {
             GameObject temp = laneCars [randomArg];
             temp.SetActive (true);
             temp.transform.position = transform.position;
             temp.GetComponent<ObstacleCarsBehavior> ().SetItActive ();
             print (gameObject.name + ": SPAWN" + temp.name);
             spawnNow = false;
         } 
         else 
         {
             print ("Choosing again: " + gameObject.name);
             ChooseCarToActivate ();
         }
 
     }

So, laneCars is the pool array. First the function checks if the currently selected object is already active. If not, it activates it. But, the SetActive(true) ain't working. It doesn't activates the object it is printing. I would really appreciate if can anyone 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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Socapex · Dec 31, 2015 at 07:42 PM

So, since laneCars is an array of prefabs, you need to Instantiate them. In unity, you can't just enable a prefab, since it doesn't exist. Replace your SetActive line with an instantiate line. Something like Instantiate(temp);

You can set the postition and rotation of instantiated objects to. Think of Instantiate as a constructor (if you are used to other OOP languages). More info http://docs.unity3d.com/ScriptReference/Object.Instantiate.html

Hope this helps :)

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 dakshesh1010 · Jan 01, 2016 at 03:17 PM 0
Share

But, that's not even the problem. temp.SetActive(true) in the 7th line doesn't seem to work.

The print on the 10th line is being executed, and I can see that output on the console. But the SetActive() function is not activating the gameobjects itself.

avatar image Socapex dakshesh1010 · Jan 01, 2016 at 06:43 PM 0
Share

Can you post what is laneCars? Is it a list/array of GameObjects dragged from the inspector (guessing here)?

I am a little bit shooting in the dark, as this is quite strange to me. Unless there is something obvious that we are both missing :)

avatar image
0

Answer by dakshesh1010 · Jan 01, 2016 at 09:55 PM

laneCars is an array of prefabs. There are few lanes in my game, and each lane is responsible for itself to generate a car at some random intervals (an empty gameObject to control it). There are 5 types of cars (obstacles) in my game. So I created an array of size 10 and populated this array in the inspector randomly by drag and drop for each lane. I hope this gives a better understanding of my problem.

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 dakshesh1010 · Jan 01, 2016 at 09:56 PM 0
Share

@Socapex Sorry I added it as an answer ins$$anonymous$$d of a comment.

avatar image Socapex dakshesh1010 · Jan 01, 2016 at 11:00 PM 0
Share

No problem, I edited my answer which should make it work. Unless I really didn't understand the problem.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

List resulting in out of range 0 Answers

Trying to program two buttons to appear when the player in my game dies 0 Answers

Spawning random tiles 1 Answer

Delete GameObject on a specific Grid Location 1 Answer

How do I spawn multiple objects in a limited area, at the start of the game? 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