- Home /
why cant I place the building prefab even though I have the correct amount of money?
hello, I have a script that allows me to place buildings on buildplaces (areas that allow me to place my buildings on) and I'm supposed to only be able to place these buildings if the variable MyMoney, (which is constantly going up) equals 10. however, it won't allow me to place buildings even when the MyMoney variable equals 10. (I'm referencing the MyMoney variable from a script called MoneyManager) can someone help me? here is my script:
using UnityEngine;
using System.Collections;
public class Buildplace : MonoBehaviour
{
// The Tower that should be built
public GameObject factoryPrefab;
private MoneyManager moneyManager;
void OnMouseUpAsButton()
{
if (moneyManager.GetComponent<MoneyManager>().MyMoney >= 10.0f)
{
// Build Tower above Buildplace
GameObject g = (GameObject)Instantiate(factoryPrefab);
g.transform.position = transform.position + Vector3.up;
}
}
}
here is the MoneyManager script where I get the MyMoney variable from:
using UnityEngine;
using System.Collections;
public class MoneyManager : MonoBehaviour
{
public float MyMoney;
IEnumerator Money()
{
// suspend execution for 5 seconds
yield return new WaitForSeconds(5);
MyMoney += 1.0f;
yield return StartCoroutine("Start");
}
IEnumerator Start()
{
// Start function WaitAndPrint as a coroutine
yield return StartCoroutine("Money");
}
}
Answer by HellsHand · May 02, 2021 at 02:13 AM
This line of code, moneyManager.GetComponent<MoneyManager>().MyMoney >= 10.0f
, would give a NullReference error
if it actually ran, so one of your issues is your OnMouseUpAsButton()
method can't be firing. Second, once it does fire as I said it will throw a NullReference
because moneyManager
is only referring to a type and not an actual script. You have to get a script by passing in a script/GameObject to a public MoneyManager moneyManager
, or getting the object it's on and using GetComponent<MoneyManager>()
is two ways to go about it.
Answer by WerewoofPrime · May 02, 2021 at 11:40 PM
hello HellsHand! thank you for answering me. can you please give me some example code? I have tried all of the ways I could possibly think of.... nothing will work I keep getting the NullReference error like you have said.
Another way to go about it, if there is only 1 MoneyManager
in your game, is to add something like this to your MoneyManager
script:
public static MoneyManager mm;
void Awake()
{
mm = this;
}
With that you can call MoneyManager.mm.MyMoney
from BuildPlace
but MoneyManager
must be on an active object in your scene.
Answer by suIly · May 03, 2021 at 05:43 PM
public GameObject factoryPrefab;
public MoneyManager moneyManager; //You'll need to assign this in the inspector
void OnMouseUpAsButton()
{
if (moneyManager.MyMoney >= 10.0f)
{
GameObject g = (GameObject)Instantiate(factoryPrefab);
g.transform.position = transform.position + Vector3.up;
}
}
The problem was, you can't use "GetComponent" on an object that you are trying to fetch. You have to reference that object, or use "GetComponent" on a GameObject.
Your answer
Follow this Question
Related Questions
Change prefab before instantiation 1 Answer
Updating a variable on a script in an instanced object 1 Answer
How to reset a variable? 1 Answer
How to store a prefab in a variable 1 Answer
Putting instantiate inside a variable 2 Answers