0

Hello everyone I have added a toggle button to my game scene that sends the Boolean value to a function called turbine1 :

using UnityEngine;
using System.Collections;
using Assets.Code.PowerPlants;

public class hydroProwerControlPanel : MonoBehaviour {

    private HydroElectric hydro;

    public hydroProwerControlPanel (){
        hydro = new HydroElectric();
    }

    public void turbine1State (bool t1) {
        hydro.t1Bool = t1;

        Debug.Log (t1);
        Debug.Log (hydro.ControlPanel());
    }
}

In another script I have a GUI box that calls the function ControlPanel () too but the result is different of the one I get when I call the same function on the Debug.Log above:

hydro.t2Bool = GUI.Toggle (new Rect (25, 195, 100, 50), hydro.t2Bool, "Turbina 1 MW");

hydro.t3Bool = GUI.Toggle (new Rect (25, 235, 100, 50), hydro.t3Bool, "Turbina 0.5 MW");

GUI.Box (new Rect (Screen.width - 310, 6, 50, 25), hydro.ControlPanel ().ToString ());

The t2Bool and t3Bool work well but they are using the old UI method. To finalize here is the object Hydroelectric along with its function ControlPanel:

public HydroElectric ()
        {   
     t1Bool = true;
     t2Bool = true;
     t3Bool = false;

     prod = 0f; 
    }

    public float ControlPanel ()
    {
        turbina1 = t1Bool ? 1.5F : 0;
        turbina2 = t2Bool ? 1 : 0;
        turbina3 = t3Bool ? 0.5F : 0;

        prod = turbina1 + turbina2 + turbina3;
        return prod;
    }

The values I receive in the Debug.Log are correct, but the GUI.Box does not considers the t1Bool that is being sent by the new UI method. Can you help? I hope I made myself clear.

The t1Bool Boolean is working perfectly but when I do this:

GUI.Box (new Rect (Screen.width - 310, 6, 50, 25), hydro.ControlPanel ().ToString ());     
Debug.Log (hydro.ControlPanel());

ControlPanel() function returns 2 different values.

Grow Animation
  • 163
  • 4
  • 13
  • hi Grow, you cannot use the ancient Unity "gui" system. It is deprecated and does not work. Fortunately it is absolutely easy to use the normal system. (1) add "Canvas" (2) add "Checkbox". you're done. – Fattie Mar 05 '16 at 17:49
  • Possible duplicate of [How do I toggle a checkbox in Unity with code?](http://stackoverflow.com/questions/33954683/how-do-i-toggle-a-checkbox-in-unity-with-code) – Fattie Mar 05 '16 at 17:50
  • But the t1Bool which is the one with the problem it was done using the new system – Grow Animation Mar 05 '16 at 17:55
  • the checkbox works, and so is the Bool it sends, the problem is that the function ControlPanel() does not consider it when placed in another script. – Grow Animation Mar 05 '16 at 17:56
  • huge number of QA on this http://stackoverflow.com/a/25932315/294884 – Fattie Mar 05 '16 at 18:08

0 Answers0