Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[solved]activate Specific Element In Array ?

Discussion in 'Scripting' started by skyLark9, Apr 13, 2019.

  1. skyLark9

    skyLark9

    Joined:
    May 2, 2018
    Posts:
    135
    Hi
    In my array there are 6 elements "guns" and I want to activate one element after deactivate all of them. Here is my script
    Code (CSharp):
    1.     public saveInfo _file; // get gun ID number from save file.
    2.     public GameObject Player1_pistol;
    3.     public GameObject[] pl1_p; // 6 guns
    4.  
    5.     void Start()
    6.     {
    7.         pl1_p = new GameObject[Player1_pistol.transform.childCount];
    8.         for (int p = 0; p < Player1_pistol.transform.childCount; p++)
    9.         {
    10.             pl1_p[p] = Player1_pistol.transform.GetChild(p).gameObject;
    11.             pl1_p[p].SetActive(false);
    12.           // Now I need to activate the third element in my array.
    13.         }
    14.     }
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    Simply set them active based on an int

    Code (CSharp):
    1.     public GameObject Player1_pistol;
    2.     public GameObject[] pl1_p; // 6 guns
    3.  
    4.     public int GunIndexToActivate = 2;
    5.  
    6.     void Start()
    7.     {
    8.         pl1_p = new GameObject[Player1_pistol.transform.childCount];
    9.         for (int p = 0; p < Player1_pistol.transform.childCount; p++)
    10.         {
    11.             pl1_p[p] = Player1_pistol.transform.GetChild(p).gameObject;
    12.             pl1_p[p].SetActive(p == GunIndexToActivate);
    13.         }
    14.     }
     
    skyLark9 likes this.
  3. skyLark9

    skyLark9

    Joined:
    May 2, 2018
    Posts:
    135
    Thank you WarmedxMints.