c# Unity3D Using toggle button to change boolean in another script and return a sum

ClaudioA

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?

Foggzie

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

JQuery - change text of button, while toggle another object, using on()

From Dev

Attach new script to button onclick event in unity3d c#

From Dev

How to access enums and variables from another C# script Unity3d

From Dev

How to access enums and variables from another C# script Unity3d

From Dev

is it possible toggle bool at editor by script? (Unity3D)

From Dev

Making a gradient and change colors based on that gradient in Unity3D - C# Script

From Dev

Button change color on toggle using jQuery

From Dev

Unity3D Running A Script Inside Another Script

From Dev

How to link a GUI Texture button to a script in Unity3d

From Dev

Toggle boolean using function?

From Dev

Unity3d StartCoroutine from another script

From Dev

How to change toggle button state from another activity

From Dev

How to change Switch/toggle button state from another activity

From Dev

How to change toggle button state from another activity

From Dev

Toggle button back change when another row click

From Dev

Calling script from script in C# Unity3D

From Java

How to make a method change another method's return statement (boolean)

From Dev

How to make a method change another method's return statement (boolean)

From Dev

change background toggle button

From Dev

Change Image with toggle button

From Dev

Unity3d declare different script as 'using ___'

From Dev

The 'this' keyword in Unity3D scripts (c# script, JavaScript)

From Dev

How to change the CSS of the active toggle button using pseudo CSS transitions

From Dev

Pop Up Button Cell return a boolean using Cocoa Binding

From Dev

Using FXML and JavaFX, how do I set a boolean variable using toggle button?

From Dev

Java/Android - How can I make a toggle button change state by pressing another button?

From Dev

Return button text after toggle

From Dev

Return button text after toggle

From Dev

Using function to change color based on Boolean value in another function in JS

Related Related

  1. 1

    JQuery - change text of button, while toggle another object, using on()

  2. 2

    Attach new script to button onclick event in unity3d c#

  3. 3

    How to access enums and variables from another C# script Unity3d

  4. 4

    How to access enums and variables from another C# script Unity3d

  5. 5

    is it possible toggle bool at editor by script? (Unity3D)

  6. 6

    Making a gradient and change colors based on that gradient in Unity3D - C# Script

  7. 7

    Button change color on toggle using jQuery

  8. 8

    Unity3D Running A Script Inside Another Script

  9. 9

    How to link a GUI Texture button to a script in Unity3d

  10. 10

    Toggle boolean using function?

  11. 11

    Unity3d StartCoroutine from another script

  12. 12

    How to change toggle button state from another activity

  13. 13

    How to change Switch/toggle button state from another activity

  14. 14

    How to change toggle button state from another activity

  15. 15

    Toggle button back change when another row click

  16. 16

    Calling script from script in C# Unity3D

  17. 17

    How to make a method change another method's return statement (boolean)

  18. 18

    How to make a method change another method's return statement (boolean)

  19. 19

    change background toggle button

  20. 20

    Change Image with toggle button

  21. 21

    Unity3d declare different script as 'using ___'

  22. 22

    The 'this' keyword in Unity3D scripts (c# script, JavaScript)

  23. 23

    How to change the CSS of the active toggle button using pseudo CSS transitions

  24. 24

    Pop Up Button Cell return a boolean using Cocoa Binding

  25. 25

    Using FXML and JavaFX, how do I set a boolean variable using toggle button?

  26. 26

    Java/Android - How can I make a toggle button change state by pressing another button?

  27. 27

    Return button text after toggle

  28. 28

    Return button text after toggle

  29. 29

    Using function to change color based on Boolean value in another function in JS

HotTag

Archive