Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Bug Variable does not change to the indicated value on the script

Discussion in 'Scripting' started by Lex_212, Mar 6, 2023.

  1. Lex_212

    Lex_212

    Joined:
    Oct 3, 2020
    Posts:
    84
    Hi, I have the following script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5. using UnityEngine.UI;
    6.  
    7. public class ElementoEnTabla : MonoBehaviour
    8. {
    9.     public Elementos elemento;
    10.     public TMP_Text símbolo;
    11.     public TMP_Text masa;
    12.     public TMP_Text atom;
    13.  
    14.     private Button botonElemento;
    15.     public Button resetButton;
    16.     public ElementoFinal pizarraNormal;
    17.  
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.         botonElemento = GetComponent<Button>();
    22.         símbolo.text = elemento.símbolo;
    23.         masa.text = elemento.masaAtómica.ToString();
    24.         atom.text = elemento.númeroAtómico.ToString();
    25.     }
    26.  
    27.     // Update is called once per frame
    28.     void Update()
    29.     {
    30.         botonElemento.onClick.AddListener(SetElemento);
    31.         resetButton.onClick.AddListener(ResetFormula);
    32.     }
    33.  
    34.     public void SetElemento()
    35.     {
    36.         for (int i = 0; i < pizarraNormal.elements.Length; i++)
    37.         {
    38.             if (string.IsNullOrEmpty(pizarraNormal.elements[i]) && !pizarraNormal.elementosUsados.Contains(elemento))
    39.             {
    40.                 pizarraNormal.elements[i] = elemento.símbolo;
    41.                 pizarraNormal.elementosUsados.Add(elemento);
    42.                 pizarraNormal.cantidadElemento[i] += 1;
    43.                 break; // Break out of For-Loop
    44.             }
    45.             else
    46.             {
    47.                 if (elemento.símbolo == pizarraNormal.elements[i])
    48.                 {
    49.                     pizarraNormal.cantidadElemento[i] += 1;
    50.                     break;
    51.                 }
    52.             }
    53.         }
    54.     }
    55.  
    56.     public void ResetFormula()
    57.     {
    58.         pizarraNormal.elements[0] = "";
    59.         pizarraNormal.elements[1] = "";
    60.         pizarraNormal.elements[2] = "";
    61.  
    62.         pizarraNormal.cantidadElemento[0] = 0;
    63.         pizarraNormal.cantidadElemento[1] = 0;
    64.         pizarraNormal.cantidadElemento[2] = 0;
    65.         pizarraNormal.elementosUsados.Clear();
    66.     }
    67. }
    The void "SetElemento" (which conteins a for loop) is executed once an UI button is pressed. What I do first is to check if a string in an array has a value or not and if a list that I created contains "elemento". In case that the string is empty and the list does not contain "elemento", the script should add elemento.símbolo to the string, add "elemento" to the list and add 1 to an int variable. This part of the code works fine. The problem comes with the toher part of the for loop. In case that the string is not empty and the list contains "elemento", all that it has to do is add 1 to the int variable, but when I try it the script adds another value to the variable (for example: 198). What I find weird is that once I click an UI button to reset everything (using the void ResetFormula) and then click the initial button again, the value is in every casebigger that before. (real example: 198, 603, 956, 1412).

    I really don't know where the problem is, but I know that if I change the line 49 to "= 1" instead of "+= 1" the value does change to 1 and if change it to "-= 1" the same happends, but the values keeps decreasing.
     
  2. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    446
    Code (csharp):
    1. void Update()
    2.     {
    3.         botonElemento.onClick.AddListener(SetElemento);
    4.         resetButton.onClick.AddListener(ResetFormula);
    5. }
    Isn't this just going to add another new onClick listener event to the button on every single Update frame/tick? So the more times that Update runs, the more listener events get added to the button.
    No wonder you are getting massive increases/decreases if this is the case as every time Update runs it is adding another listener event.
    Try moving those two lines into the Start method and see if that then works as you would expect.
     
    mopthrow, Lex_212 and Kurt-Dekker like this.
  3. Lex_212

    Lex_212

    Joined:
    Oct 3, 2020
    Posts:
    84
    Thanks! Don't know how I missed that