- Home /
how do you access and use a variable from a separate script then run an if statement depending on the value of that variable?
hello, I am making a city building game where you can place buildings and factories on buildplaces. what I have been trying to do is make it so you may only place the buildings when you have enough money. however, the MyMoney variable that I am trying to check is in a separate script. how could I get access to the MyMoney variable in a script that it was not declared in, and then check its value?here is the script that I have tried... please help! I am not the best at coding c# so, please give me example code that I could use. i have spent hours and cannot figure it out.
using UnityEngine;
using System.Collections;
public class Buildplace : MonoBehaviour
{
// The Tower that should be built
public GameObject factoryPrefab;
private MoneyManager moneyScript;
// Start is called before the first frame update
void Start()
{
moneyScript = GetComponent<MoneyManager>();
}
void OnMouseUpAsButton()
{
if (moneyScript.MyMoney >= 10)
{
// Build Tower above Buildplace
GameObject g = (GameObject)Instantiate(factoryPrefab);
g.transform.position = transform.position + Vector3.up;
}
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Randomizer code not working. 1 Answer
Accessing gameobject from other scene 2 Answers
UnityEngine.Rigidbody does not contain a definition for `contraints' 1 Answer