Search Unity

i dont know how script this xD

Discussion in 'Scripting' started by matiasgj97, Apr 26, 2019.

  1. matiasgj97

    matiasgj97

    Joined:
    Nov 26, 2018
    Posts:
    1
    hi everyone, i have a question about this script. my purpose is to insert in different gameobjects with the same caracteristics. that when changed the boolean value, obtains a value. For example. I have 5 GameObects with bool x = false, if the value changes ( x= true) then i obtain the total of gameobjects that value is true. if 3 GO are true and another GO is false NTOTAL is equal to 3.

    this script is the best way i found to try this out... im new at programming with c# xDDD

    i need some help :)
    Code (CSharp):
    1. public class obejotasif : MonoBehaviour {
    2.  
    3.     // Use this for initialization
    4.     protected int NTOTAL;
    5.     public int _arrayAccess;
    6.     private int[] x;
    7.     private bool _funcion;
    8.     void Start () {
    9.         x = new int[3];
    10.      
    11.         if (_funcion == true)
    12.         {
    13.             x[_arrayAccess] = 1;
    14.         }
    15.         else
    16.         {
    17.             x[_arrayAccess] = 0;
    18.         }
    19.      
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update () {
    24.         if (Input.GetKeyDown("space"))
    25.         {
    26.             _funcion = true;
    27.             GETTOTAL();
    28.             Debug.Log(NTOTAL);
    29.          
    30.         }
    31.         else
    32.         {
    33.             _funcion = false;
    34.         }
    35.  
    36.  
    37.      
    38.     }
    39.     private void GETTOTAL()
    40.     {
    41.         NTOTAL = x[0] + x[1] + x[2] + x[3] +x[4];
    42.     }
    43. }
     
  2. supermoof

    supermoof

    Joined:
    Sep 24, 2015
    Posts:
    45
    I'm assuming you need something like this?

    Code (CSharp):
    1.     obejotasif[] objectArray;
    2.     int counter = 0;
    3.  
    4.     foreach (obejotasif item in objectArray)
    5.     {
    6.         if(item.booleanValue)
    7.         {
    8.             counter++;
    9.         }
    10.     }
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    4,696
    I'm not 100% sure what your script above is trying to do. If you have a collection, you just need to loop through, count the number of true values and then display that value. With smaller collections, this works perfectly fine.

    And looks like @supermoof coded a basic example. You just may need to reset the counter to 0 if you need a fresh count each time.