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;
}
}
}