0
\$\begingroup\$

I saw this question asked some times but no one correlates to mine. I see people using the GetComponent() function but this one should work too.

First I receive a Bool value from a toggle button and assign it as a field of the object 'hydro':

hydroControl.cs

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

public class hydroProwerControlPanel : MonoBehaviour {
private HydroElectric hydro;
public bool t1;

void Start () 
{ 
    t1 = true;
}

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

Then I have the object where this bool change should be recognized and sent as an output, but it doesn't:

using System;
using UnityEngine;
namespace Assets.Code.PowerPlants
{
    public class HydroElectric
    {
        public bool t1Bool;
        float turbina1;

        public float prod;

        public HydroElectric ()
        {   
          t1Bool = true;
          prod = 0f; 
        }

        public float ControlPanel ()
        {
            turbina1 = t1Bool ? 1.5F : 0;   

            prod = turbina1 ;
            Debug.Log (prod);
            return prod;
        }
    }
}

As requested, this is where the function ControlPanel() is called:

using System;
using UnityEngine;
using UnityEngine.UI;
using Assets.Code.PowerPlants;
namespace Assets.Code.Interfaces
{

public class PlayLevel1 : MonoBehaviour 
{
private HydroElectric hydro;
public Text producao;

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

public void OnGUI()
{
 producao.text = hydro.ControlPanel().ToString();          
}
}
}

Do you have any idea why does this Bool does not get updated? Any help appreciated.

\$\endgroup\$
1
  • \$\begingroup\$ Just to clarify, your asking for an explanation of the particular reason, not a means to get around it? \$\endgroup\$
    – Gnemlock
    Mar 9, 2016 at 9:23

1 Answer 1

0
\$\begingroup\$

You're actually referring to 2 different instances of HydroElectric, which is why you're not seeing the bool change value.

As it stands hydroProwerControlPanel holds an instance of HydroElectric whose value changes when you call turbineState, but PlayLevel1 holds a completely different instance of HydroElectric and that instances value is never modified.

There are a couple of ways around this. You could implement the Singleton pattern if there is only ever going to be one HydroElectric per scene, or you can pass PlayLevel1 the instance of HydroElectric that you want to monitor (known as Dependency Injection)

More details on Singletons in Unity can be found here:
https://rockonflash.wordpress.com/2010/10/21/singletons-in-unity3d/
http://wiki.unity3d.com/index.php/Singleton

More details on Dependency Injection in Unity can be found here:
http://wiki.unity3d.com/index.php/Dependency_injection
http://blogs.unity3d.com/2014/05/07/dependency-injection-and-abstractions/

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .