Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 M1xelated · Mar 11, 2015 at 09:45 AM · prefabenable

Enabling a disabled prefab in the scene

hi,

I am loading a prefab into my scene. By default this gameobject is disabled. I am stuck on enabling this gameobject from another script. Since it isn't in the scene by default I cannot make a public gameobject and assign it. I tried getting the gameobject by name and tag but haven't been succesful.

Anyone got any clues on this ?

Thanks

Marc

Comment
Add comment · Show 5
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 Ekta-Mehta-D · Mar 11, 2015 at 09:56 AM 0
Share

Can you post your code which you have tried ?

avatar image Bonfire-Boy · Mar 11, 2015 at 12:08 PM 0
Share

The most efficient way (i.e. avoiding searching for the object) would be to have a reference to the GameObject as a member of some other object in the scene (perhaps in the script which is going to be used to enable it). This reference would start off as null, and you would set it to your new object when you instantiate it.

avatar image Noob_Vulcan · Mar 11, 2015 at 12:51 PM 0
Share

"I tried getting the gameobject by name and tag but haven't been succesful."

Just make sure your that Your Prefab loading is done 1st and then find the gameobject. This is the only reason u arent getting the gameobject by name.

avatar image Bonfire-Boy · Mar 11, 2015 at 12:55 PM 0
Share

It may be worth mentioning one common pitfall when searching for a runtime-instantiated object by name, which is failing to find it due to searching for the name of the Prefab. Ins$$anonymous$$d you should explicitly set the name after instantiation, and use that name in your search. But better to avoid searching altogether as per my previous comment.

avatar image M1xelated · Mar 11, 2015 at 01:53 PM 0
Share

Thanks, I fixed it by creating a new gameobject as a parent. This one is always active and used for referencing. I then just toggled the child on and off. It is still done by searching for now, but I will get back on that when optimizing. Thanks for the help guys !

2 Replies

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

Answer by M1xelated · Mar 11, 2015 at 02:23 PM

I fixed it by creating a new gameobject and setting the gameobject that I want to disable as its child. This apparantly is the only option when trying to enable it when not instantiated at start

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 guneyozsan · Feb 17, 2017 at 01:14 PM 0
Share

I had a case where I had an active Prefab but one of its children should be disabled by default when instantiated. Then I needed to activate that Child later in the game. Since it is impossible to "Find" an inactive Child or assign a component of a Prefab to a variable before that Prefab is instantiated, the only way I could solve this was to leave the Child active in the Prefab, attach a script to the Child or Parent that assignes the Child to a variable in a script of a Parent and then disables the Child.

avatar image
-1

Answer by hav_ngs_ru · Mar 11, 2015 at 10:09 AM

 public class AnotherScript {
    public GameObject gameObjectToEnable; // assign it in inspector
 
    public Start() { 
       // for testing we activate it iat startup.
       // later write it where it shoule be written
       gameObjectToEnable.SetActive(true); 
    }
 }


then just add some GO in you scene, attach this script to it, drag-drop your "GO from prefab" to gameObjectToEnable field and start a game.

I guess some confusing between prefab and gameobject in scene. so if this will not work - show us your project.

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 Bonfire-Boy · Mar 11, 2015 at 12:05 PM 0
Share

But the OP says that the object "isn't in the scene by default" and hence "I cannot make a public gameobject and assign it" which I think is explicitly ruling out your solution. I think that the object is being instantiated at run time here.

avatar image M1xelated · Mar 11, 2015 at 02:15 PM 1
Share

"Since it isn't in the scene by default I cannot make a public gameobject and assign it" The thing is that the GameObject is not loaded into the scene from the start. I did however find the solution. I created a parent that is always on and its child can be toggled. I googled it and it appears that there is no way to search for disabled gameobjects.

Thanks anyways !

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

activating a prefab in scene 1 Answer

Disabling Canvas Prefab only turns off prefab, not in scene canvas 1 Answer

Using raycast to enable canvas that is linked to a prefabs gameobject 0 Answers

How to Enable and Disable Prefabs -1 Answers

Prefab disables a script out of runtime. 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