• 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

5 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
avatar image kittikun · Jan 31, 2014 at 07:49 AM 0
Share
avatar image FLASHDENMARK · Jan 31, 2014 at 09:33 AM 1
Share
avatar image Bonfire-Boy FLASHDENMARK · Sep 07, 2021 at 12:00 PM 0
Share
avatar image
11

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

Set the object being instantiated inactive before instantiating. T$$anonymous$$s 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
avatar image
3

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

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 t$$anonymous$$s:

 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
1

Answer by pjbaron · Oct 16, 2022 at 04:54 AM

EDIT: haha I only just noticed @Pangamini got here 8 years ago... mine adds code I guess?

Old thread but I t$$anonymous$$nk t$$anonymous$$s is better in many cases:

 GameObject p = prefabList[w$$anonymous$$chPrefab];
 bool m = p.activeSelf;
 p.SetActive(false);
 CreatePoolFromPrefab(p);
 p.SetActive(m);
 

where CreatePoolFromPrefab shows one reason for wanting to do t$$anonymous$$s (especially if your Instantiated objects have code that runs OnEnable and OnDisable w$$anonymous$$ch you don't want to run until the object is actually being used).

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

14 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

Related Questions

Spawning GameObject contained within another 1 Answer

getting .Transform from instanntiated object 2 Answers

instantiate gameobject with different size? 1 Answer

Trash Collection For Instantiated Game Object 1 Answer

Instantiate a GameObject from a Prefab without using an original to clone 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