Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check If active Multiple gameobject

I looking for a way to check multiple gameobject (numbers) and 5 parts to display, every part have 3 numbers they display random (0 is in all part), if O active in part1 do not display 0 in part 2,3,4 and 5 again but randomize another numbers in that part(2,3,4,5) it means I want to display 0 random in one part only in scene (either part 1,2,3,4or 5) and another number to random also. enter image description here

I tried with the code below but I don't know what I am missing please help

private GameObject[] checkfirst;

[SerializeField]
private List < GameObject > objects = new List < GameObject > ();

public GameObject firstziro;

void Start()

{

  foreach(GameObject go in checkfirst) {
    if (go.activeSelf) {

      firstziro.gameObject.SetActive(false);

      int randomlyActiveObject = UnityEngine.Random.Range(0, objects.Count);
      for (int i = 0; i < objects.Count; i++) {
        objects[i].SetActive(i == randomlyActiveObject);
      }
    } else {

      firstziro.gameObject.SetActive(true);
      foreach(GameObject d in objects) {
        d.gameObject.SetActive(false);
      }

    }
  }
like image 401
betrice mpalanzi Avatar asked Mar 08 '26 22:03

betrice mpalanzi


1 Answers

Do not check all five part your checking 15 object into script is hard to handle, create the script to check only is section before random, and then create another script to random 0, enter image description here

[SerializeField] 
  private List<GameObject> objects = new List<GameObject>(); 





public GameObject five; 

  void Start() 
  { 
      if( five.gameObject.activeSelf) 
      { 
           
foreach (GameObject g in objects) 
{ 
  g.gameObject.SetActive(false); 
} 

      } 
      else{ 
              int randomlyActiveObject = UnityEngine.Random.Range(0, objects.Count); 
for (int i = 0; i < objects.Count; i++) 
{ 
  objects[i].SetActive(i == randomlyActiveObject); 
} 
} 


      } 
  }
like image 142
monkey from Tanzania Avatar answered Mar 10 '26 11:03

monkey from Tanzania



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!