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
1
Question by valichm · Apr 07, 2012 at 07:13 AM · gameobjectinstantiateinactive

Instantiate inactive object

I'm trying to spawn an object in one of many locations using a vector3 array, but keep it inactive until some action is taken. How can you instantiate an inactive object?

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

Answer by DaveA · Apr 07, 2012 at 07:20 AM

Set it inactive immediately.

 var newGO = Instantiate (......
 newGO.active = false;
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 jkgd · Jan 21, 2013 at 10:07 AM 3
Share

This still runs Awake(), I believe.

avatar image kittikun · Jan 31, 2014 at 07:49 AM 0
Share

Yes and OnEnabled, etc. is ran too, so this is not a good solution. There is no better alternative I believe...

avatar image FLASHDENMARK · Jan 31, 2014 at 09:33 AM 1
Share

If you do not want Awake, OnEnabled etc. to execute than put all of your code from those respective functions into a custom function. As an example take your Awake() code and put it into CustomAwake() and only call that function when you need to.

avatar image Bonfire-Boy FLASHDENMARK · Sep 07, 2021 at 12:00 PM 0
Share

This is the key, for me. If having Awake being called before the object is needed is a problem, then one is probably doing things in Awake that ought to be done in Start.


Not clear to me why OnEnabled would be a problem (since OnDisabled will be called when you disable the object).


I've used this pattern quite a bit, eg having a component's Awake function register it with a manager (so that it can be turned on again when needed) and then disabling it immediately so the Start doesn't get called yet.

avatar image
9

Answer by Pangamini · Jan 31, 2014 at 10:44 AM

Set the object being instantiated inactive before instantiating. This will work with asset and scene prefabs

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 svendkiloo · Dec 15, 2014 at 08:23 AM 1
Share

Does this mean untick the "active" tick in the prefab? This is what I had done as a workaround earlier, but it means that the icons/preview of the prefab in Project view (Two Column Layout) is blank, which is a rather significant side effect. I've tried programmatically setting active on the prefab to false, but that doesn't seem to do anything?

avatar image
3

Answer by Santifocus · Sep 05, 2021 at 12:00 PM

The 3 answers given are all not really helpfull because 1. Setting the active state AFTER Instantiating still will call Awake / OnEnable 2. Settings the active state BEFORE Instantiating will change prefabs 3. Having to always make sure that a prefab root object is disabled will cause unnecessary effort and is honestly anoying.

Therefore if you want to instantiate a object disabled use this:

 public static class UnityUtils
 {
     /// <summary>
     /// Will instantiate an object disabled preventing it from calling Awake/OnEnable.
     /// </summary>
     public static T InstantiateDisabled<T>(T original, Transform parent = null, bool worldPositionStays = false) where T : Object
     {
         if (!GetActiveState(original))
         {
             return Object.Instantiate(original, parent, worldPositionStays);
         }
         
         (GameObject coreObject, Transform coreObjectTransform) = CreateDisabledCoreObject(parent);
         T instance = Object.Instantiate(original, coreObjectTransform, worldPositionStays);
         SetActiveState(instance, false);
         SetParent(instance, parent, worldPositionStays);
         Object.Destroy(coreObject);
         return instance;
     }
     
     /// <summary>
     /// Will instantiate an object disabled preventing it from calling Awake/OnEnable.
     /// </summary>
     public static T InstantiateDisabled<T>(T original, Vector3 position, Quaternion rotation, Transform parent = null) where T : Object
     {
         if (!GetActiveState(original))
         {
             return Object.Instantiate(original, position, rotation, parent);
         }
         
         (GameObject coreObject, Transform coreObjectTransform) = CreateDisabledCoreObject(parent);
         T instance = Object.Instantiate(original, position, rotation, coreObjectTransform);
         SetActiveState(instance, false);
         SetParent(instance, parent, false);
         Object.Destroy(coreObject);
         return instance;
     }
     
     private static (GameObject coreObject, Transform coreObjectTransform) CreateDisabledCoreObject(Transform parent = null)
     {
         GameObject coreObject = new GameObject(string.Empty);
         coreObject.SetActive(false);
         Transform coreObjectTransform = coreObject.transform;
         coreObjectTransform.SetParent(parent);
 
         return (coreObject, coreObjectTransform);
     }
 
     private static bool GetActiveState<T>(T @object) where T : Object
     {
         switch (@object)
         {
             case GameObject gameObject:
             {
                 return gameObject.activeSelf;
             }
             case Component component:
             {
                 return component.gameObject.activeSelf;
             }
             default:
             {
                 return false;
             }
         }
     }
 
     private static void SetActiveState<T>(T @object, bool state) where T : Object
     {
         switch (@object)
         {
             case GameObject gameObject:
             {
                 gameObject.SetActive(state);
 
                 break;
             }
             case Component component:
             {
                 component.gameObject.SetActive(state);
 
                 break;
             }
         }
     }
 
     private static void SetParent<T>(T @object, Transform parent, bool worldPositionStays) where T : Object
     {
         switch (@object)
         {
             case GameObject gameObject:
             {
                 gameObject.transform.SetParent(parent, worldPositionStays);
 
                 break;
             }
             case Component component:
             {
                 component.transform.SetParent(parent, worldPositionStays);
 
                 break;
             }
         }
     }
 }
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
2

Answer by cheesejunkie · Oct 15, 2015 at 06:37 PM

Set the script on your prefab to disabled (untick the box on the script) and make sure it's saved that way. Keep in mind that you have to save your scene to make the prefab changes persist. Unticking the box simply prevents all Unity methods from being called EXCEPT Awake. Don't use awake, use Start or OnEnable for initiation.

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

13 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

Related Questions

How can I add the OnTriggerEnter function to all game objects that I instantiate? 1 Answer

Instantiating GO infront of player 2 Answers

Souls pickup mechanic (Like Dark Souls, BloodBourne) 0 Answers

Toggling a game object between an active and inactive state 1 Answer

Instantiate Terrain Object as child of Empty Game Object 1 Answer


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