Search Unity

A button that change the value of a displayed variable

Discussion in 'Immediate Mode GUI (IMGUI)' started by ayosha, Apr 13, 2015.

  1. ayosha

    ayosha

    Joined:
    Apr 7, 2015
    Posts:
    9
    Hello everybody,

    I am struggling with that issue for way too much time and since I tried pretty much everything, I would like to request your help on this situation.

    What I want to do is simply a button that change the value of a displayed text when it is clicked.

    [EDIT : Check my answer (third post) for a better explaination and pictures of my issue]

    When I compile, I don't get any error but when I click on the button, I got this :
    Someone can please help me on that ? I just want my button to change the displayed text on the screen, each time I click on it. I am sure there is a very simple way to do it, but I really tried a lot of things and the solution won't come out! :/

    Thanks in advance! :)
     
    Last edited: Apr 14, 2015
  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    In the screenshot you posted, there's nothing set on the Text Nb_gob T2 field of the tototo object... (it shows none).

    Also, the naming of your variables are very confusing (to me), making it somewhat hard to follow.
     
    ayosha likes this.
  3. ayosha

    ayosha

    Joined:
    Apr 7, 2015
    Posts:
    9
    Yes, sorry about that; It wasn't clear.
    Here you can see clearly my environment :


    Now, here is my script Nb_gobT that is attached to Object_Onclick_NbgobT, that will be launched when the button Tunnel (in orange in the screen) will be clicked. The script just launched another script that will increase the text of the amount of recruted goblins.

    Here is Nb_gobT :

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using System;
    5.  
    6. public class Nb_gobT : MonoBehaviour {
    7.  
    8.     public Text textNb_gobT;
    9.     public int intNb_gobT = 0;
    10.     public Nb_gobTtext _var;
    11.  
    12.     void Awake()
    13.     {
    14.     //    _var = GameObject.Find("GameObject2").GetComponent<Nb_gobTtext>();
    15.     //    _var.TextNb_gobT2 = GetComponent<Text>();
    16.  
    17.     }
    18.  
    19.     public void Tunnelclick()
    20.     {
    21.         _var.DisplayNumberGob();
    22.     //    intNb_gobT += 1;
    23.     //    _var.TextNb_gobT2.text = intNb_gobT.ToString();
    24.     }
    25. //    }
    26.  
    27. }
    28.  
    And some more of my environment :


    So here, we see the object "Objet_onclick_nbgobT is fully filled, and "Var" which is a Nb_GobText object (the script that change the amount) is filled too.

    Here is the Nb_GobText script that is launched by "Object_Onclick_NbgobT" :
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using System;
    5.  
    6. public class Nb_gobTtext : MonoBehaviour {
    7.  
    8.     public Text TextNb_gobT2;
    9.     public int intNb_gobT;
    10.     // Use this for initialization
    11.     void Start() {
    12.     TextNb_gobT2 = GetComponent<Text>();
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     public void DisplayNumberGob () {
    17.         intNb_gobT += 1;
    18.         TextNb_gobT2.text = intNb_gobT.ToString();
    19.     }
    20. }
    21.  
    And the environment of the second object :


    When I click, I have the error message above. I've tried so hard for the past few days.... Still the same error, and the number of goblins won't increase in my tunnels :(
     
    Last edited: Apr 15, 2015
  4. ayosha

    ayosha

    Joined:
    Apr 7, 2015
    Posts:
    9
    Problem fixed! I remove the line " TextNb_gobT2 = GetComponent<Text>(); " as I didn't need to specify an object since I was already specifying one in the inspector! :p