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.

To properly format your code use the 101 010 button. For more information on how to effectively use Unity Answers watch the Unity Answers tutorial https://www.youtube.com/watch?v=ezAPpViLs2Q and read the faq: http://answers.unity3d.com/page/faq.html.

2 Answers

2

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);

alright, thank you

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*.gameObject.SetActive (true);*

}
Also, your ‘NullReferenceException’ error is coming up from somewhere else, cuz the above code will give the compilation error first.

Ill make sure to try this instead