I have this interface buttons in my playstate.cs:
HydroElectric.t1Bool = GUI.Toggle (new Rect (25, 55, 100, 50), HydroElectric.t1Bool, "Turbina 2 MW");
HydroElectric.t2Bool = GUI.Toggle (new Rect (25, 95, 100, 50), HydroElectric.t2Bool, "Turbina 3 MW");
HydroElectric.t3Bool = GUI.Toggle (new Rect (25, 135, 100, 50), HydroElectric.t3Bool, "Turbina 1 MW");
and also this box:
GUI.Box (new Rect (Screen.width - 100, 60, 80, 25), HydroElectric.prod.ToString ());
Then I have this HydroElectric.cs script where if conditions are verified to change the value of a SUM called prod:
using UnityEngine;
namespace Assets.Code.PowerPlants
{
public class HydroElectric
{
public HydroElectric ()
{
bool t1Bool = true;
bool t2Bool = true;
bool t3Bool = false;
float prod = 0;
int turbina1;
int turbina2;
int turbina3;
}
public void HydroControlPanel (bool t1Bool, bool t2Bool, bool t3Bool, int turbina1, int turbina2, int turbina3, float prod)
{
if (t1Bool == true)
{
turbina1 = 2;
}
else
{
turbina1 =0;
}
if (t2Bool == true)
{
turbina2 = 3;
}
else
{
turbina2 =0;
}
if (t3Bool == true)
{
turbina3 = 1;
}
else
{
turbina3 =0;
}
prod = turbina1 + turbina2 + turbina3;
}
}
}
I guess I would have to initialize the Hydroelectric class by creating a new variable and using the method new in the playstate and also create a return for the variable prod in the HydroControlPanel method but I am having trouble to make sense out of this and combine it with the toogle button.
Also the dot syntax is not working because the console says that t1Bool and etc does not exist in current context. I cannot declare the variable as static since they are in the constructor I think.
Do you have any idea how I can make this work?
If you cleaned up your class so that the booleans are public members instead of only existing in the scope of the constructor like so:
using UnityEngine;
namespace Assets.Code.PowerPlants
{
public class HydroElectric
{
public bool t1Bool;
public bool t2Bool;
public bool t3Bool;
int turbina1;
int turbina2;
int turbina3;
float prod;
public HydroElectric ()
{
t1Bool = true;
t2Bool = true;
t3Bool = false;
prod = 0f;
}
You can then create an instance HydroElectric ec = new HydroElectric();
and access those booleans of that instance with the dot: ec.t1Bool
.
Then you can modify your HydroControlPanel()
function to return the float
instead of storing it (of course you can always do both like below if you'd like but the scope of your question doesn't make it clear which you want).
public float HydroControlPanel ()
{
turbina1 = t1Bool ? 2 : 0;
turbina2 = t2Bool ? 3 : 0;
turbina3 = t3Bool ? 1 : 0;
prod = turbina1 + turbina2 + turbina3;
return prod;
}
}
}
Now when a boolean is changed you can call HydroControlPanel()
and it will reevaluate it's prod
and return the new value.
As a note, this syntax:
turbina1 = t1Bool ? 2 : 0;
Is a ternary operator (has 3 parameters) that can be referred to as the "in-line if." It means "If t1Bool
is true
, set turbina1
to 2; if t1Bool
is false
, set turbina1
to 0". It's a much cleaner way of grouping those if(bool) {} else {}
statements you had.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments