Search code examples
c#unity-game-enginegameobject

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

    }
  }

Solution

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