C# Assignation of bool variable not working

Hi everyone,

I have an issue concerning two scripts that are components to a same GameObject, one of this scripts calls a method ClickButton() through a UI button, here is the script of the function:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class SeedSpotGUIController : MonoBehaviour {
	public GameObject seedSpotMessage;
	public GameObject gameManager;
	public SeedingSpotShieldBehaviour seedBehaviour;
	public bool activate = false;
	//Use this for initialization
	public void Start(){
		gameManager = GameObject.FindGameObjectWithTag("GameController");
		seedSpotMessage = GameObject.Find("MainHUD").transform.Find("SeedSpotMessage").gameObject;
		seedBehaviour = this.gameObject.GetComponent<SeedingSpotShieldBehaviour> ();
	}

	//public void Update(){
	//	if (activate)
	//		seedBehaviour.Activate ();
	//}
	//Controls buttons from SeedSpotGUI
	public void ClickButton(string buttonID){
		gameManager = GameObject.FindGameObjectWithTag ("GameController");
		seedSpotMessage = GameObject.Find("MainHUD").transform.Find("SeedSpotMessage").gameObject;
		if (buttonID == "close") {
			//Close pop-up message
			seedSpotMessage.SetActive (false);
		} else {
			//Calls activation of seeding spot
			seedSpotMessage.SetActive(false);
			this.gameObject.GetComponent<SeedingSpotShieldBehaviour> ().activated = true;
			gameManager.GetComponent<PlayerInventory>().numSeeds -= 1;
		}
	}	
}

In this line of code, I’m assigning a bool which activates a coroutine in another script:

this.gameObject.GetComponent<SeedingSpotShieldBehaviour> ().activated = true;

The problem is however, the assignation is not been made, I suspect is because ClickButton() isn’t asynchronous, but I’m really not sure because:

gameManager.GetComponent<PlayerInventory>().numSeeds -= 1; 

Is been made, with the invocation of the other methods, also I’ve double-checked that seedBehaviour.activate is public, any thoughts?

Thank you.

EDIT:

I’ve condensed all 3 classes I was using, still having the same issue.

using UnityEngine;
using System.Collections;

public class SeedSpotBehaviour : MonoBehaviour {

	public Vector3 target;
	public Vector3 initial;
	public GameObject _sprout;
	public GameObject _mainHUD;
	public GameObject _gameManager;
	public bool active;
	public float growingTime = 10f;

	void Awake(){
		active = false;
	}

	// Use this for initialization
	void Start () {
		_sprout = this.gameObject.transform.Find("Sprout-Faceted").gameObject;
		_mainHUD = GameObject.Find ("MainHUD");
		_gameManager = GameObject.FindGameObjectWithTag("GameController");
		initial = new Vector3 (0, 0, 0);
		target = new Vector3 (2.5f, 2.5f, 2.5f);
	}
	
	//Update is called once per frame
	void Update () {
		if (active == true) {
			Debug.LogWarning(this.active);
			StartCoroutine (Grow ());
		}
	}

	void OnMouseDown(){
		_mainHUD = GameObject.Find ("MainHUD");
		if(!active)
		if (_gameManager.GetComponent<PlayerInventory> ().numSeeds <= 0) 
			_mainHUD.transform.Find("NoSeedsMessage").gameObject.SetActive(true);
		else
			_mainHUD.transform.Find("SeedSpotMessage").gameObject.SetActive(true);
	}

	public void MouseClickedGUISeeding(string buttonID){
		_mainHUD = GameObject.Find ("MainHUD");
		_gameManager = GameObject.FindGameObjectWithTag("GameController");
		if (buttonID.Equals ("close")) 
			_mainHUD.transform.Find ("SeedSpotMessage").gameObject.SetActive (false);
		else {
			this.active = true;
			Debug.LogWarning(this.active);
			_gameManager.GetComponent<PlayerInventory>().numSeeds -= 1;
			_mainHUD.transform.Find ("SeedSpotMessage").gameObject.SetActive (false);
		}
		Debug.LogWarning(this.active);
	}

	public void MouseClickedGUINoSeed(){
		_mainHUD = GameObject.Find ("MainHUD");
		_mainHUD.transform.Find ("NoSeedsMessage").gameObject.SetActive (false);
	}

	IEnumerator Grow(){
		float startTime = 0f;
		if(_sprout.transform.localScale.sqrMagnitude == 0)
		while (_sprout.transform.localScale.sqrMagnitude != target.sqrMagnitude) {
			Debug.LogWarning("JA!");
			Vector3 currentLerp = Vector3.Lerp (initial, target, startTime);
			Debug.LogWarning("Current Lerp:" + currentLerp);
			_sprout.transform.localScale = currentLerp;
			Debug.LogWarning(_sprout.transform.localScale);
			startTime += Time.deltaTime / growingTime;
			yield return null;
		}
	}
}

Try saving the GetComponent variable in a local variable, and then set activated to true. If still doesn’t work, print the value of activated after setting it. Also you can use the debugger to to go line by line and watch the activated property. To make sure it changes. I don’t know why you are starting the coroutine by changing the value, try calling it with the local variable, temp.StartCoroutine(Grow());.

I assume you know that the variable is not activated, but active; and you just made a typo here.

Ok I’ve finally resolved the issue, turns out that the button in the UI had a reference to the prefab, so all changes were made on the prefab, not the instances of the prefab. So I resolved this by assigning a SeedSpotManager on my Canvas:

using UnityEngine;
using System.Collections;

public class SeedSpotManager : MonoBehaviour {
	public GameObject seedSpot;
	public GameObject _gameManager;

	// Use this for initialization
	void Start () {
		_gameManager = GameObject.FindGameObjectWithTag("GameController");
	}

    //Assigns prefab instance to the MainHUD
	public void assignSeedSpot(GameObject seedSpot){
		this.seedSpot = seedSpot;
	}

    //Activates the seedSpot
	public void Activate(){
		_gameManager.GetComponent<PlayerInventory>().numSeeds -= 1;
		this.seedSpot.GetComponent<SeedSpotBehaviour> ().active = true;
	}
}

Then, I erased the coroutine and made all the coroutine instruction inside the update:

//Update is called once per frame
void Update () {
	if (active) {
		Vector3 currentLerp = Vector3.Lerp (initial, target, startTime);
		Debug.LogWarning("Current Lerp:" + currentLerp);
		_sprout.transform.localScale = currentLerp;
		Debug.LogWarning(_sprout.transform.localScale);
		startTime += Time.deltaTime / growingTime;
	}
}

Also all UI control instructions are going to be implemented inside SeedSpotManager.

Thanks to all who helped me, sorry about the really late feedback, but I have no computer in my house.

Also thanks to @Glurth for Why won’t GetComponent ().rotation.SetLookRotation actually change the object’s rotation? - Unity Answers