- Home /
Activating a game object that is inactive through inspector
I've disabled some gameobjects through setting the tick box in the inspector for these objects to off. Problem is I don't know how to activate these objects when my application is running. I tried this:
gameobject = GameObject.Find("MyObject");
gameobject.active = true;
This causes a "NullReferenceException" as the call to GameObject.Find is returning NULL. Thus I cannot set the object to active. Can anyone help me activate these objects?
Answer by Rod-Green · Oct 04, 2011 at 11:18 PM
This will find all objects of any type even if disabled/inactive. This is slow. So really either use it for preprocessing/editor scripts or cache the results.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public static class GameObjectHelper
{
public static List<T> FindObjectsOfType<T>() where T : UnityEngine.Object
{
return new List<T>((T[])GameObject.FindObjectsOfType(typeof(T)));
}
public static List<T> FindObjectsOfType<T>(bool p_includeHidden) where T : UnityEngine.Object
{
if(!p_includeHidden)
return FindObjectsOfType<T>();
return new List<T>((T[])Resources.FindObjectsOfTypeAll(typeof(T)));
}
}
The problem with this is that it returns more than simply scene objects of type.
For example, If I use this to get all objects by type GameObject, I also get prefabs that haven't been instantiated.
This becomes a problem when trying to do something like setting all inactive GameObjects to active.
Answer by runevision · Dec 20, 2010 at 10:45 AM
GameObject.Find only searches for active GameObjects.
See this related question and answer for info on how to handle the problem: http://answers.unity3d.com/questions/14178/cannot-toggle-active-on-gameobjects-that-are-inactive
Thanks. I've already looked at this answer. The problem is that my gameobjects have been deactivated through the inspector so the 'Find' to build my initial array fails as the objects start deactivated.
I'm not absolutely sure about this, but I think the point is that when you deactivate it in the editor its not available to the compiled application. So, if you want something in the project it needs to be activated in the editor and then manage active/deactive in your scripts.
Answer by Zogg · Oct 05, 2011 at 03:06 AM
The simplest solution would be to create a variable myobject and to assign manually MyObject to it in the Inspector.
public var myobject : Gameobject // Visible in the inspector, set it to MyObject
...
myobject.active = true;
This is fine for one or two objects. But in cases where you're creating editor tools or something that needs to evaluate existing objects this solution doesn't work. As it requires prior knowledge and user managed lists. Burdening the user with maintenance is a pretty bad idea.
Your answer
Follow this Question
Related Questions
Referencing children of an instantiated object. 1 Answer
Turing GameObjects On and Off again. 4 Answers
Unity Drops Var Object Assignments on Runtime 0 Answers
How to use if statement properly 1 Answer