So I am currently trying to grab the score from an object to see if it’s more than something, and I can not seem to do that, I had a massive issue with trying to get it to access the objects code the first time around, but I seem to have fixed that somehow.
Now I added the same line of code to another object and yet I am unable to get it working, I fixed most of the issues with the code, but I am still running into some issues since my code doesn’t seem to be working when it comes to checking if it’s true or not.
using UnityEngine;
using System.Collections;
public class BuyingTower : MonoBehaviour{
public MotherController motherController;
public int scoreValue;
public GameObject tower1;
public Transform buttonplace;
void Start ()
{
GameObject gameControllerObject = GameObject.FindWithTag ("GameController");
if (gameControllerObject != null)
{
motherController = gameControllerObject.GetComponent <MotherController>();
}
if (motherController == null)
{
Debug.Log ("Cannot find 'motherController' script");
}
}
public void ButtonPosition(){
getposition = transform.GetComponent<RectTransform>().position;
}
void Update ()
{
Input.GetKeyDown("Fire1");
{
if motherController.score >= 20;
{
MotherController.RemoveScore (scoreValue);
Instantiate(tower1, buttonplace.getposition = transform.GetComponent<RectTransform>().position, buttonplace.getposition = transform.GetComponent<RectTransform>().position);
}
}
}
}
The main issue I have is with this little bit;
void Update ()
{
Input.GetKeyDown("Fire1");
{
if motherController.score >= 20;
{
It works fine with adding the AddScore and that, so it’s finding the actual object, I simply can not get the if statement to work and actually check…
Here’s the code for the MotherController;
using UnityEngine;
using System.Collections;
public class MotherController : MonoBehaviour
{
public GameObject Enemies;
public Vector3 spawnValues;
public int hazardCount;
public float spawnWait;
public float startWait;
public float waveWait;
public GUIText scoreText;
private int score;
void Start ()
{
score = 0;
UpdateScore ();
StartCoroutine (SpawnWaves ());
}
IEnumerator SpawnWaves ()
{
yield return new WaitForSeconds (startWait);
while (true)
{
for (int i = 0; i < hazardCount; i++)
{
Vector3 spawnPosition = new Vector3 (spawnValues.x, spawnValues.y, spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate (Enemies, spawnPosition, spawnRotation);
yield return new WaitForSeconds (spawnWait);
}
yield return new WaitForSeconds (waveWait);
}
}
void Update ()
{
}
public void AddScore (int newScoreValue)
{
score += newScoreValue;
UpdateScore ();
}
public void RemoveScore (int newScoreValue)
{
score -= newScoreValue;
UpdateScore ();
}
void UpdateScore ()
{
scoreText.text = "Money: " + score;
}
}
Now I know it’s a bloody mess, but I’l get to cleaning it up later, right now I am simply trying to understand why it will not work, most commented code is just code that I am yet to actually clear out.
Anyone have any idea how I could solve this?