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

Question wrong property value after button clicked

Discussion in 'UGUI & TextMesh Pro' started by ghiboz, Oct 19, 2021.

  1. ghiboz

    ghiboz

    Joined:
    Sep 7, 2012
    Posts:
    465
    hello!
    this is my code:
    Code (CSharp):
    1. using TMPro;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. public class MyClass : MonoBehaviour
    6. {
    7.     [SerializeField] private int value1;
    8.     [SerializeField] private int value2;
    9.     [SerializeField] private int value3;
    10.    
    11.  
    12.     void MyFunction()
    13.     {
    14.         value1 = 10;
    15.         value2 = 20;
    16.         value3 = 30;
    17.     }
    18.  
    19.     public void CallButtonClicked()
    20.     {
    21.         Debug.Log($"{value1} {value2} {value3}");
    22.     }
    23. }
    24.  
    the event CallButtonClicked is selected in the `OnClick` list of the button: choosing the gameobject where myclass is attached and choosing the function...

    if I call MyFunction and the values take the 10, 20, 30, when I press the button, the values are 0....

    I found a workaround using the listener by code.. but is not a good stuff.. any ideas?
    thank you
     
  2. JuliaP_Unity

    JuliaP_Unity

    Unity Technologies

    Joined:
    Mar 26, 2020
    Posts:
    696
    Hello, I moved your post to the UGUI Forum since it's not using UI Toolkit (where you originally posted it).

    You should check the values on the Inspector inside Unity for the values of value1, value2 and value3, those are the values that will appear when you click the button. Also in this code there's no explicit call to MyFunction(), so make sure you're actually calling it.
     
  3. ghiboz

    ghiboz

    Joined:
    Sep 7, 2012
    Posts:
    465
    thank you Julia!
    the MyFunction is only for information: the values shown in editor are 10, 20, 30, and the debug.log write me 0 0 0