0

I have a script A that receives a bool input (bool t1) from a toggle button and sets a bool field (hydro.t1Bool) with the bool received from the toggle like this:

HydroElectric hydro;
public bool t1;

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

I have a script B with a function (inside the HydroElectric object) that returns a value according to this field Bool:

public float ControlPanel ()
        {
            turbina1 = t1Bool ? 1.5F : 0;
            prod = turbina1 ;//+ turbina2 + turbina3;
            return prod;
        }

Then I have a script C that calls the ControlPanel() function from the object Hydroeletric and shows it on screen:

producao.text = hydro.ControlPanel().ToString();

My problem is that I need to make this hydro.t1Bool static for this dynamic to work. But to make t1Bool static I need to make its class static, but then I can't have a constructor (which I have) I can't declare instance members and my script A won't work because I can't declare variables of static types.

How can I make t1Bool static and avoid all this limitations?

Grow Animation
  • 163
  • 4
  • 13
  • 2
    You don't need to mark class as `static` to have static members in it. – Sinatr Mar 07 '16 at 16:29
  • 1
    It seems that some of your used classes (script is a bit wrong word to use whether to denote the classes themselves or the files they are contained in) need to get instances of some another classes as dependencies. They can get such dependencies as parameters in their constructors - http://programmers.stackexchange.com/questions/177649/what-is-constructor-injection – Eugene Podskal Mar 07 '16 at 17:06
  • this ***SHOULD NOT BE A STATIC*** and has no connection to statics. It is almost inconceivable that as a new hobbyist programmer you will need to use statics. indeed you are ***TOTALLY "BREAKING" THE WHOLE IDEA OF HOW UNITY WORKS!!!!*** Just "find" the game object / component as has been explained 18 billion times. or simply ***drag an Inspector variable***. it just could not be easier. – Fattie Mar 07 '16 at 17:13
  • Possible duplicate of [Accessing a variable from another script C#](http://stackoverflow.com/questions/25930919/accessing-a-variable-from-another-script-c-sharp) – Fattie Mar 07 '16 at 17:15
  • if you must have a static Class somewhere ... **so, that's a "global"** .. for example to hold scores or the user's nickname or whatever .. do it. but you can't have statics in `MonoBehaviour`, in `Component`s. it's nonsensical and won't work. – Fattie Mar 07 '16 at 17:18
  • @joeBlow How do I make this without using statics? – Grow Animation Mar 07 '16 at 17:35
  • notice large link to duplicate question. https://www.youtube.com/watch?v=wWKhZUBFhUo – Fattie Mar 07 '16 at 17:47

1 Answers1

0

You don't need to have the class be declared as static to have static members

    public class HydroElectric
    {
        public static string t1Bool { get; set; }
    }

But having static members on a non static class is bad practice. In OOP classes are meant to hide its members.

James Dev
  • 2,979
  • 1
  • 11
  • 16