- Home /
SetActive is giving me a lot of issues
So Ive used GameObject.active = true
for quite a long time, however I am trying to get to learn SetActive as its updated and preferred, however I have kindof an issue when I try to use it, here is the script var obj1 : GameObject[]; function Start() { obj1 = GameObject.FindGameObjectsWithTag("TheObj1"); if (PlayerPrefs.GetInt("ObjOn")==1) { obj1.GameObject.SetActive (true); } else if (PlayerPrefs.GetInt("ObjOn")==0) { obj1.GameObject.SetActive (false); } }
and the error is: NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args) UnityScript.Lang.UnityRuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args, System.Type scriptBaseType) ObjOnJV.Start () (at Assets/Standard Assets/Character Controllers/Sources/Scripts/Camera/ObjOnJV.js:9)
c# insists on writing Gameobject like this:
obj1.gameObject.SetActive (true)
(lower case at the beginning). I don't know if US has the same limitation.
Answer by Lachee1 · Jun 12, 2015 at 05:49 AM
You object is null. The game cannot find any objects with the tag "TheObj1". Make sure the object exists at the beginning of the game and it is tagged correctly. Maybe manually assign the object in the inspector instead of relying on the FindObjectWithTag function.
--edit--
You are also calling SetActive from obj1.GameObject, so you are getting the static class GameObject. You want to write it in lower case, like obj1.gameObject.SetActive(true);
I seem to be having a new error however, I changed it to both gameObject, and removed the FindObject function and just manually did it, however now i am receiving this error:
NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cache$$anonymous$$eyName, System.Type[] cache$$anonymous$$eyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cache$$anonymous$$eyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args) UnityScript.Lang.UnityRuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args, System.Type scriptBaseType) ObjOnJV.Start () (at Assets/Standard Assets/Character Controllers/Sources/Scripts/Camera/ObjOnJV.js:7)
Answer by superbsumit · Jun 12, 2015 at 05:56 AM
Your implementation is wrong. Your 'obj1 ' variable is an array of GameObjects, so doing this ' obj1.GameObject.SetActive (true);' won't work. It's not a valid line of code and it will not compile at all. You should SetActive for an 'array' of objects as this:
for(var i : int = 0; i < obj1.Length; i++){
obj1[i].gameObject.SetActive (true);
}
Also, your 'NullReferenceException' error is coming up from somewhere else, cuz the above code will give the compilation error first.
Your answer
Follow this Question
Related Questions
NullReferenceException 3 Answers
NullReferenceException Error 6 Answers
GameObject[] Issues 1 Answer
NullReferenceException: Object reference not set to an instance of an object 0 Answers