Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by WerewoofPrime · May 02, 2021 at 12:35 AM · instantiatevariable

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");
        
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image suIly · May 03, 2021 at 05:53 PM 0
Share

@WerewoofPrime I answered your question ( i think )

avatar image HellsHand · May 03, 2021 at 08:10 PM 0
Share

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.

avatar image
0

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;
 
      }            
  }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image suIly · May 03, 2021 at 05:44 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

144 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges