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 MonkeyAssassin8 · Sep 16, 2010 at 11:32 AM · moneyaddingupincorrectly

How to add money?

I have chests that open and give me money. When I open the first chest it gives me 500, but when I open a second chest it is adding the new amount to the original money amount of 0 and I am still left with 500. here is my code please help:

var money : int; var GUIMoney : GUIText; var target : Transform; var moneyAmount : int;

function OnTriggerEnter( other : Collider ) { if (other.tag == "Player") { money+= moneyAmount; GUIMoney.text = money.ToString(); Destroy(other.gameObject); } }

function Update(){ MoneyPU(); }

function MoneyPU(){ distance = Vector3.Distance(target.transform.position, transform.position);

     if(distance < 2){
         if(Input.GetButtonDown("Activate")){
             yield WaitForSeconds(1);
             money+= moneyAmount; 
             GUIMoney.text = money.ToString();
             Destroy (this);

         }
     }

}

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
3
Best Answer

Answer by spinaljack · Sep 16, 2010 at 12:37 PM

You need to store "money" and "moneyAmount" in different scripts (or instances) because at the moment each script is updating its own money amount which will always be 0. Store the player's money on the player object or a game controller object separate from the pickups.

e.g.

money script:

var moneyAmount : int = 500;

function OnTriggerStay( other : Collider ) { // check for player tag and active button if (other.tag == "Player" && Input.GetButtonDown("Activate")) { // send a message to the player object other.gameObject.SendMessage("AddMoney",moneyAmount); // removed the money Destroy(gameObject); } }

player script:

var money : int = 0; var GUImoney : GUIText;

function AddMoney(amount :int){ money += amount; GUIMoney.text = money.ToString(); }

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

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

No one has followed this question yet.

Related Questions

Add Fresh Script to Game Object 1 Answer

Add GameObject to GameObjectArray[] 3 Answers

How to assign a gameobject to a script that is added dynamically 1 Answer

How to add script to objects through Empty Child? (or other easier ways) 2 Answers

How to write a script which move the character when the it entered an area? 1 Answer


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