0

I am making my first game, and I am having trouble updating a bool value using a toggle button.

This is the button:

using UnityEngine;
using Assets.Code.Interfaces;
using Assets.Code.Scripts;
using Assets.Code.PowerPlants;

namespace Assets.Code.States

public void ShowIt()
{
    HydroElectric.t1Bool = GUI.Toggle (new Rect (25, 55, 100, 50), HydroElectric.t1Bool, "Turbina 2 MW");
    GUI.Box (new Rect (Screen.width - 100, 60, 80, 25), HydroElectric.prod.ToString ()); // PRODUCED ENERGY 
}

This is where I have the if condition:

using System;

namespace Assets.Code.PowerPlants

public class HydroElectric
{
    public static bool t1Bool = true;
    public int turbina1;
    public static float prod = 0;

    public HydroElectric ()
    {
        if (t1Bool == true)
        {
            turbina1 = 2;
        }
        else
        {
            turbina1 =0;
        }

        prod = turbina1;
    } 
}

I have no errors on the console, the bool value is changing (I used debug) but the value of turbina1 is not changing (when I debug it from the first script). I have no idea why its value is not changing along with the true or false condition. Any idea?

2 Answers 2

1

In your HydroElectric class the constructor takes the value of t1Bool at the time that the object is created and uses it to calculate the value of prod. After that point there is nothing in your code that will change the value of prod when the value of t1Bool changes.

Instead of setting the value once you could have it calculate the prod from the value of t1Bool each time you read it:

public class HydroElectric
{
    public static bool t1Bool = true;
    public static float prod
    {
        get
        {
            return t1Bool ? 2f : 0f;
        }
    }
}

Alternatively you can set the value of prod whenever the value of t1Bool changes:

public class HydroElectric
{
    private static bool _t1Bool = true;
    public static bool t1Bool 
    {
        get { return _t1Bool; }
        set
        {
            _t1Bool = value;
            prod = value ? 2f : 0f;
        }
    }

    public static float prod = 2f;
}

This is slightly faster, but only enough so to make a difference when you are checking the value of prod very frequently.

There are other ways to respond to changing state including event handlers and implicit notifier lists. Just remember that if you want something to happen you have to make it happen, and that assigning a value type (simple things like numbers and booleans as well as complex struct types) won't keep updating them over time.

Sign up to request clarification or add additional context in comments.

3 Comments

But you understand that I use a toggle button to manipulate the value of turbina 1 right? Your code is a bit advanced for me, I feel I have to study a bit to understand. Also I have also turbina 2 and turbina 3. All three are summed in prod.
Also, why you used float?
You only showed turbina1 in your sample code, and didn't reference it outside the constructor so I ignored it. If you need those values, add them. The code itself is fairly simple property get/set stuff which you should learn because it is very common in C#.
1

Your bool condition is executed in the constructor, which means that HydroElectric.t1Bool will not work because turbina1 is already set when HydroElectric is instantiated. Put your bool condition in a separate method and call that from your script.

Comments

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.