Search Unity

SetActive False Multiple GameObjects

Discussion in 'Scripting' started by drndfx, Oct 30, 2020.

  1. drndfx

    drndfx

    Joined:
    Jul 3, 2013
    Posts:
    89
    Hello,
    Similar setup as Unity's Roll-A-Ball project where you destroy cubes when you roll the ball.
    The next time when you start a game how do you SetActive=False the cubes that are already collected?
    Without creating 100-200 "if" statements like below?

    Note: I already setup enum on another script where it sets cube1=true.

    Code (CSharp):
    1. public List<GameObject> myObjects = new List<GameObject>();
    2. void Start()
    3.     {
    4. if (cube1 == true)
    5.         {
    6.             myObjects[0].SetActive(false);
    7.         }
    8. if (cube2 == true)
    9.         {
    10.             myObjects[1].SetActive(false);
    11.         }
    12. ...............................
    13. }
     

    Attached Files:

  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Instead of having separate variables like cube1, cube2, etc., create a List to hold all of those values. Then you can use a loop to iterate over the whole list, and modify
    myObjects[i]
    based on the value of
    cube[i]


    Any time you find yourself creating variables whose names end in 1, 2, etc. that almost always means you should be using a List instead.
     
  3. drndfx

    drndfx

    Joined:
    Jul 3, 2013
    Posts:
    89
    Thank you. I can’t wrap my head around using for loop to filter out the specific objects.

    I have 550 cubes and I want to filter out only the cubeOrange, cubeBlue, cubeGreen etc and set them as SetActive false

    for (int i = 0; i = 550; i++) {
    //Stuff
    }

    and without using game Tags. Is it possible?
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Add some other identifier to the objects then. Check for that identifier, and act on it. For example, it could be a public variable on a script attached to the objects.
     
  5. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    The closest thing to the code in your OP would be something like
    Code (CSharp):
    1. for (i = 0; i < myObjects.Count; ++i)
    2. {
    3.     if (cube[i] == true) myObjects[i].SetActive(false);
    4. }
    Though notice that that loop never calls SetActive(true) on anything. If you want to make it so that the cubes with 'true' bools become inactive and all the rest become active, you could change that to
    Code (CSharp):
    1. for (i = 0; i < myObjects.Count; ++i)
    2. {
    3.     myObjects[i].SetActive(!cube[i]);
    4. }
    Both of those assume that you've already got a bool variable for every single cube, and the values are controlled by some other code somewhere. That's one possible way of doing things.

    Another approach would be to have a list of just the cubes you want to remove; e.g.
    List<GameObject> cubesToRemove;


    And then your loop might look more like
    Code (CSharp):
    1. for (i = 0; i < myObjects.Count; ++i)
    2. {
    3.     myObjects[i].SetActive(!cubesToRemove.Contains(myObjects[i]));
    4. }
    The best way to organize your data will depend on how that data is being generated or updated.
     
  6. drndfx

    drndfx

    Joined:
    Jul 3, 2013
    Posts:
    89
    Thank you for the answers. I was also looking into using Enums and it allowed me to get results I wanted. Something like this.


    Code (CSharp):
    1. public enum ItemsBricks
    2.                      {
    3.                          cube1, cube2.........................etc
    4.                      }
    5.  
    6.    void checkForDestroyedBricks(ItemsBricks state)
    7.    {
    8.        if (state == ItemsBricks.cube1)
    9.         {
    10.             this.gameObject.SetActive(false);
    11.         }
    12.    }