So, I have a toggle button to change a bool value, in this script 't1Bool'. The toggle button is working because when I use a Debug.log I can see the value changing from true o false when I press the button. When using the Debug.log with the variable turbina1 the value is always zero. I already checked the if condition syntax and don't know why this value is not being updated.

using System;
using UnityEngine;
namespace Assets.Code.PowerPlants
    public class HydroElectric
        public bool t1Bool;
        public bool t2Bool;
        public bool t3Bool;

        int turbina1;
        int turbina2;
        int turbina3;

        public float prod;

        public HydroElectric ()
         t1Bool = true;
         t2Bool = true;
         t3Bool = false;
         prod = 0f; 

        public float HydroControlPanel ()
            turbina1 = t1Bool ? 2 : 0;
            turbina2 = t2Bool ? 3 : 0;
            turbina3 = t3Bool ? 1 : 0;

            prod = turbina1 + turbina2 + turbina3;
            Debug.Log (turbina1);
            return prod;

This is the code for the button:

public void ShowIt()
            ec.t1Bool = GUI.Toggle (new Rect (25, 55, 100, 50), ec.t1Bool, "Turbina 2 MW");

            ec.t2Bool = GUI.Toggle (new Rect (25, 95, 100, 50), ec.t2Bool, "Turbina 3 MW");

            ec.t3Bool = GUI.Toggle (new Rect (25, 135, 100, 50), ec.t3Bool, "Turbina 1 MW");

            GUI.Box (new Rect (Screen.width - 100, 60, 80, 25), ec.prod.ToString ());      // PRODUCED ENERGY


This is where the object instance is created (it is in the same script as the button):

public class PlayState : IStateBase
        private StateManager manager;
        private HydroElectric ec;
        public PlayState (StateManager managerRef)
            manager = managerRef;
            Debug.Log ("Constructing PlayState");
            ec = new HydroElectric();

Any hint?

  • \$\begingroup\$ turbina1 is marked as static, are you sure you meant for it to be static? \$\endgroup\$ – Draco18s no longer trusts SE Dec 28 '15 at 20:07
  • \$\begingroup\$ it seems not, I've already removed it but the problem persists. \$\endgroup\$ – ClaudioA Dec 28 '15 at 20:11
  • \$\begingroup\$ The problem is with only turbina1 or with all three? \$\endgroup\$ – Hamza Hasan Dec 28 '15 at 20:14
  • \$\begingroup\$ Can we see the code for the button? \$\endgroup\$ – Draco18s no longer trusts SE Dec 28 '15 at 20:14
  • \$\begingroup\$ The problem is with all three. I've just added the code for the button. \$\endgroup\$ – ClaudioA Dec 28 '15 at 20:15

In last line, it should be ec.HydroControlPanel().ToString() instead of ec.prod.ToString() in ShowIt method

| improve this answer | |
  • 1
    \$\begingroup\$ To explain: the button updated the internal on/off state, but HydroControlPanel() which calculates the total output based on that internal on/off state is never called, thus prod is never updated. \$\endgroup\$ – Draco18s no longer trusts SE Dec 28 '15 at 20:26
  • \$\begingroup\$ @Draco18s thanx man, m on cell phone. Thanx for explaining. And u r welcome ClaudioA :) \$\endgroup\$ – Hamza Hasan Dec 28 '15 at 20:36
  • \$\begingroup\$ @HamzaHasan No problem. I know the joys (read "troubles") of trying to type anything longer than four words on a mobile device (mine has a quirk dating back a few months where the learned dictionary words got completely wiped, along with a number of real words, like "do." I cannot type "do" on my tablet except through painstaking manuality, if I forget I get "so" "Co" "dip" "drop" or something else). \$\endgroup\$ – Draco18s no longer trusts SE Dec 28 '15 at 20:40
  • \$\begingroup\$ @Draco18s A big LOL :D \$\endgroup\$ – Hamza Hasan Dec 28 '15 at 20:42
  • \$\begingroup\$ @HamzaHasan Its also conveniently stopped learning new words 9.9 Right pain in the arse. Used to take it about six to ten usages of a word or proper noun for me to be able to use it in swipe-typing ("continuous input") but since the "update" I haven't seen it learn a single new word. \$\endgroup\$ – Draco18s no longer trusts SE Dec 28 '15 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.