Search Unity

Individualize variables in elements on list.

Discussion in 'Scripting' started by dama1994, Sep 23, 2020.

  1. dama1994

    dama1994

    Joined:
    Aug 9, 2018
    Posts:
    19
    Hello.
    How are you?
    I have a question with my list. I have some variables within a list that I want to be rounded up when I pass the limit. The problem with having a for is that I don't know how to tell you that if any variable in one of my list elements exceeds the file I put in so that it is rounded, I rounded only that one, not the whole list. I leave you the code, to see if you can help me.

    By the way, the method "OperationMines", is applied in an Invoke Repeating, every second.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6.  
    7.  
    8. public class CaracteristicasMinas : MonoBehaviour
    9. {
    10.    
    11.     [System.Serializable]
    12.     public struct Mejoras
    13.     {
    14.         public int siguienteMejora;
    15.     }
    16.  
    17.     [System.Serializable]
    18.     public class Minas
    19.     {
    20.         public Sprite image;
    21.         public string nombre;
    22.         public float produccion, almacen, multi, mejora, habitantes;
    23.         public TextMeshProUGUI panelTexto;
    24.        
    25.        
    26.     }
    27.     public List<Minas> recursos = new List<Minas>();
    28.  
    29.     public void FuncionamientoMinas()
    30.     {
    31.  
    32.         for (var i = 0; i < recursos.Count; i++)
    33.         {
    34.             recursos[i].produccion = recursos[i].produccion + (recursos[i].multi * recursos[i].habitantes);
    35.             //recursos[i].produccion = Mathf.Ceil(recursos[i].produccion);
    36.             recursos[i].panelTexto.text = recursos[i].produccion.ToString();
    37.             if(recursos[i].produccion > 10.000)
    38.             {
    39.                  // recursos[i].panelTexto.text = (recursos[i].produccion / 1000).ToString()+"K";
    40.             }
    41.             if (recursos[i].almacen < recursos[i].produccion)
    42.             {
    43.                 recursos[i].produccion = recursos[i].almacen;
    44.                 recursos[i].panelTexto.text = recursos[i].produccion.ToString();
    45.             }
    46.            
    47.         }
    48.  
    49.     }
    50.  
    51. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    Are you speaking about the rounding operation, as done by this API:

    https://docs.unity3d.com/ScriptReference/Mathf.Round.html

    Or are you speaking about "Wrapping", as in passing 10 and returning to 0? That is not rounding.

    Everything on a computer is done one item at a time, generally through a for() loop or other iterating construct.
     
  3. dama1994

    dama1994

    Joined:
    Aug 9, 2018
    Posts:
    19
    Hi. Thanks for answering.
    The problem I have is that I want that if some of the variables of my elements, is passed for example of 10.000, alone I changed that automatic one, without having to select each one, since I will have many and it would not be efficient. Thank you.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    I'm sorry, I am unable to understand what you want.

    How to report problems productively in the Unity3D forums:

    http://plbm.com/?p=220
     
  5. dama1994

    dama1994

    Joined:
    Aug 9, 2018
    Posts:
    19
    Hello.
    If it is difficult to express myself, since I am Spanish and use the translator.

    I'm going to show it to you with images, to see if it is clearer:
    I have this in the inspector, which are a list of a class with some parameters, then when creating an element, it creates one with all those variables (The code of this is in the first post). What I want to obtain, is that the float that is in the following image, surpasses the 10,000 in any variable of "Production", in all the elements that I have in my list, is changed only the one that surpasses it, A VARIABLE WITHIN AN ELEMENT, NOT ALL THE VARIABLES OF ALL THE ELEMENTS OF MY LIST, since I use a for, so that it checks all at the same time, but that it does not change all, either stopping the for or something.
    The concept is that I write a code that makes it encompass all my list and if it exceeds a limit in one, make the change in one, not in all and that I don't have to select all the elements of my list to be done individually: EJ "recursos[0], recursos[1]..."

    Thank you.
    upload_2020-9-25_12-3-51.png
    upload_2020-9-25_13-20-50.png

    Thank you!