Search Unity

GameObject.SetActive with Array

Discussion in 'Scripting' started by chrisswingsx, Jan 20, 2020.

  1. chrisswingsx

    chrisswingsx

    Joined:
    Oct 28, 2018
    Posts:
    19
    Hello, I am making a store system in my game where you can buy characters and in each character I have 2 buttons one with the price to buy that is in setactive state (true) by default and the other that is a check that is in setactive (false) and that when the purchase comes the buy button disappears and the check button appears I have 10 characters so I created a public GameOBject [] Hide to hide the buttons once purchased and another public GameOBject [] Toshow to show the check that they are in a for, in the inspector I drag my buttons to the public variable of the array and at the moment of buying let's say the first one that is in index [0] the same thing happens automatically with the 10 characters and I know that it is because I am going from 1 in 1 in my for. My question is how to stop when the first one stops and does not apply to other objects and when I buy the second or fifth only the setactive is applied in those index [] Please help me. I leave my code here.

    Code (CSharp):
    1. public class ShopManager : MonoBehaviour
    2. {
    3.     public GameObject[] Hide;
    4.     public GameObject[] Toshow;
    5.  
    6.     public void CharacterButtonPressed(int prices)
    7.     {
    8.         if(Singleton.instance.totalCoins>prices)
    9.         {
    10.             Singleton.instance.totalCoins -= prices;
    11.             BuyPressedTrue();
    12.             BuyPressedFalse();
    13.             Debug.Log("Acabas de comprar");
    14.         }else
    15.         {
    16.             {
    17.                 if(Singleton.instance.totalCoins<prices)
    18.                 {
    19.                     Debug.Log("No tienes dinero");
    20.                 }
    21.             }
    22.         }  
    23.     }
    24.    
    25.     public void BuyPressedFalse()
    26.     {
    27.         for (int i = 0; i < Hide.Length; i++)
    28.         {
    29.             Hide[i].SetActive(false);
    30.         }
    31.     }
    32.     public void BuyPressedTrue()
    33.     {
    34.         for (int i = 0; i < Toshow.Length; i++)
    35.         {
    36.             Toshow[i].SetActive(true);
    37.         }
    38.     }
    39. }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    I'm not 100% I understand, but if you just want to turn on a certain checkbox, just have your method take an index and turn on that checkbox when you tap on it.

    If you mean you are looping through your stuff and want to know which ones to turn on, then that is where you'll need to track it somehow to determine which should be on or off.
     
    chrisswingsx likes this.