- Home /
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?
Answer by DaveA · Apr 07, 2012 at 07:20 AM
Set it inactive immediately.
var newGO = Instantiate (......
newGO.active = false;
Yes and OnEnabled, etc. is ran too, so this is not a good solution. There is no better alternative I believe...
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.
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.
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
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?
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;
}
}
}
}
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.
Your answer
Follow this Question
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