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 MattDahEpic · Apr 22, 2014 at 11:46 PM · script errorlocal variable

There is already a local variable error. Thrown when updating a variable 2 diffrent times in a script

I have coded a script that keeps track of a money balance when buying various things. I have defined the money variable at the beginning of the script with public var money : float = 1000; with 1000 being the starting balance. Updating the variable later in the script with var money = money - fieldCost works perfectly fine for subtracting fieldCost from money, but when using that and, later in the script var money = money - gardenCost to subtract gardenCost from money, compiler throws: NullReferenceException: Object reference not set to an instance of an object on the line of the var money = money - gardenCost. (yes, its a farm game) I might be just noobing this, as I am one, but it is the only thing preventing my game from running and I just can't figure it out!

I haven't yet written the code that sets the boughtField and boughtGarden to true, should that matter.

Code:

 #pragma strict
 
 public var money : float = 1000;
 public internal var boughtField : boolean = false;
 public var fieldCost : float = 150;
 public internal var boughtGarden : boolean = false;
 public var gardenCost : float = 100;
 
 function Start () {
 
 }
 
 function Update () {
     //print balance to console
 //    Debug.Log("CurrentBalance is " + money);
     //run buyplot function
     BuyPlot ();
 }
 
 // processes when player has bought something
 function BuyPlot () {
     //has the player bought a field?
     if (boughtField == true) {
         //do they have enough money to buy a field
         if (money >= fieldCost) {
             //enough money, purchase field
             var money = money - fieldCost;
         }
         //not enough money, sends message
         else {
             Debug.Log("Not enough money to purchase Field!");
         }
     }
     
     //has the player bought a garden?
     if (boughtGarden == true) {
         //do they have enough money
         if (money >= gardenCost) {
             //enough money, purchase garden
             var money = money - gardenCost;
         }
         //not enough money, send message
         else {
             Debug.Log("Not enough Money to buy Garden!");
         }
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by getyour411 · Apr 22, 2014 at 11:48 PM

Remove "var" from the inner references

Example

 var money = money - fieldCost;

to this

money = money - fieldCost;

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 MattDahEpic · Apr 22, 2014 at 11:54 PM 0
Share

it worked! thanks!

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

20 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

Related Questions

How to fix error BCE0005: Unknown identifier 1 Answer

Converting from js to c# 1 Answer

What is wrong in this script? 1 Answer

In the editor, the script works fine, but in the build, no! 0 Answers

the associated script cannot be loaded 0 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