1

I am using Unity 5. The set up is the following:

  • A camera.
  • A 3D plane and a canvas with 3 UI Buttons.
  • A Script in c# attached to the camera. Such script contains only 2 functions: Start() and Update().

Here is the script:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ChangeButtonsText : MonoBehaviour {

    public GameObject Menu;

    private Transform[] transforms;
    private List<string> texts;
    private int numButtons;
    private int menuIndex;

    // Use this for initialization
    void Start () {
        texts = new List<string> ();
        for (int i = 0; i < 10; i++) {
            texts.Add("I am the button number " + i.ToString());
        }
        int numButtons = Menu.transform.childCount;
        print ("1 numButtons = " + numButtons.ToString ());
        transforms = new Transform[numButtons];
        for (int i = 0; i < numButtons; ++i) {
            transforms[i] = Menu.transform.GetChild(i);
        }
        print ("2 numButtons = " + numButtons.ToString ());

        menuIndex = 0;
        for (int i = 0; i < numButtons; i++) {
            transforms[i].GetComponentInChildren<UnityEngine.UI.Text>().text = texts[menuIndex];
            if((i < numButtons-1) && (menuIndex < texts.Count - 1)) menuIndex++;
        }
        print ("3 numButtons = " + numButtons.ToString ());


    }

    // Update is called once per frame
    void Update () {
        print ("4 numButtons = " + numButtons.ToString ());

        if(Input.GetKeyDown(KeyCode.Q)) {
            print ("5 numButtons = " + numButtons.ToString ());

            if (menuIndex < texts.Count - 1) menuIndex++;
            print (menuIndex.ToString());
            int j = 0;
            print ("6 numButtons = " + numButtons.ToString ());
            print ("Going in!");
            for (int i = 0; i < numButtons; i++) {
                print ("I'm in!");
                transforms[i].GetComponentInChildren<UnityEngine.UI.Text>().text = texts[menuIndex-j];
                print ("Changed!");
                j++;
            }
        }
    }
}

The 3 firsts prints regarding numButtons show 3. However, the 4th print (the first on in the Update() function prints a 0. Any idea why is this happening?

Thank you very much.

1 Answer 1

4

Inside your Start() method, you're declaring a new variable called numButtons, hiding the instance member with the same name. To fix it, simply remove the int before it, then it will assign the member and not the local variable:

for (int i = 0; i < 10; i++) {
    texts.Add("I am the button number " + i.ToString());
}
numButtons = Menu.transform.childCount; // no "int" here
print ("1 numButtons = " + numButtons.ToString ());
Sign up to request clarification or add additional context in comments.

1 Comment

Oh... I cannot believe I did that mistake. Maybe I was too sleepy hahah Okay, thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.