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?
Set the object being instantiated inactive before instantiating. This will work with asset and scene prefabs
The 3 answers given are all not really helpfull because
- Setting the active state AFTER Instantiating still will call Awake / OnEnable
- Settings the active state BEFORE Instantiating will change prefabs
- 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;
}
}
}
}
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.
EDIT: haha I only just noticed @Pangamini got here 8 years ago… mine adds code I guess?
Old thread but I think this is better in many cases:
GameObject p = prefabList[whichPrefab];
bool m = p.activeSelf;
p.SetActive(false);
CreatePoolFromPrefab(p);
p.SetActive(m);
where CreatePoolFromPrefab shows one reason for wanting to do this (especially if your Instantiated objects have code that runs OnEnable and OnDisable which you don’t want to run until the object is actually being used).
Set it inactive immediately.
var newGO = Instantiate (......
newGO.active = false;