Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

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:
    712
    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