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

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.

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.

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;

     }            
 }