- Home /
Trying to SetActive specific children based on public static int
The following script is attached to a GameObject with many children (Unity 4.6 GIU text objects). I want to turn them on (child welcomeText in this case) or off based on a public static int nextNum which I already have working. I'd like to stick to using the public static int nextNum as I want to sequence animation events aswell as the text objects. The program runs but then pauses with the following error, plus a number of objects in the scene disappear.
using UnityEngine;
using System.Collections;
public class welcomeTextControl : MonoBehaviour
{
void Start ()
{
GameObject.Find("welcomeText").SetActive(false);
}
void Update ()
{
if (SetButInc.nextNum == 1)
{
GameObject.Find("welcomeText").SetActive(true);
}
else
{
GameObject.Find("welcomeText").SetActive(false);
}
}
}
Error: NullReferenceException: Object reference not set to an instance of an object welcomeTextControl.Update () (at Assets/Scripts/welcomeTextControl.cs:20)
Comment
Best Answer
Answer by neeeb · Oct 07, 2014 at 12:47 PM
Managed to fix it;
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class welcomeTextControl : MonoBehaviour
{
public GameObject welcomeText;
void Start()
{
welcomeText.SetActive (false);
}
void Update ()
{
if (SetButInc.nextNum == 1)
{
welcomeText.SetActive(true);
}
else
{
welcomeText.SetActive(false);
}
}
}