Question by
rapinnz · Jan 04, 2018 at 10:50 AM ·
scripting problemscriptingbasicsscriptableobjectscript errorscriptable object
Help with a C# script .SetActive
this script is meant to display a number when an other number passes by. plz help -thanks
using UnityEngine;
using System.Collections;
public class Weel : MonoBehaviour
{
public GameObject UINumb0;
public GameObject UINumb1;
public GameObject UINumb2;
public GameObject UINumb3;
public GameObject UINumb4;
void Start ()
{
UINumb0.SetActive (true);
UINumb1.SetActive (false);
UINumb2.SetActive (false);
UINumb3.SetActive (false);
UINumb4.SetActive (false);
}
void OnEnterTrigger(Collider other)
{
if (other.gameObject.tag == "1")
{
Debug.Log ("Detected = 1");
UINumb1.SetActive (true);
UINumb0.SetActive (false);
UINumb2.SetActive (false);
UINumb3.SetActive (false);
UINumb4.SetActive (false);
}
if (other.gameObject.tag == "2")
{
Debug.Log ("Detected = 2");
UINumb2.SetActive (true);
UINumb0.SetActive (false);
UINumb1.SetActive (false);
UINumb3.SetActive (false);
UINumb4.SetActive (false);
}
if (other.gameObject.tag == "3")
{
Debug.Log ("Detected = 3");
UINumb3.SetActive (true);
UINumb0.SetActive (false);
UINumb1.SetActive (false);
UINumb2.SetActive (false);
UINumb4.SetActive (false);
}
if (other.gameObject.tag == "4")
{
Debug.Log ("Detected = 4");
UINumb4.SetActive (true);
UINumb0.SetActive (false);
UINumb1.SetActive (false);
UINumb2.SetActive (false);
UINumb3.SetActive (false);
}
}
}
Comment
Best Answer
Answer by OneCept-Games · Jan 04, 2018 at 10:52 AM
Restructure. Put your UINumb in a List and loop it with foreach() instead.
Something like:
using UnityEngine; using System.Collections;
public class Weel : $$anonymous$$onoBehaviour
{
public GameObject[] UINumb;
void Start ()
{
if (UINumb.Length > 0)
UINumb [0].SetActive (true);
foreach (GameObject num in UINumb)
num.SetActive (false);
}
void OnEnterTrigger(Collider other)
{
foreach (GameObject num in UINumb)
num.SetActive (false);
UINumb[int.Parse(other.gameObject.tag)].SetActive (true);
}
}
You then have to drag your 4 Week objects into the UINumb array in the Inspector.