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
1
Question by HolBol · May 17, 2010 at 04:43 PM · scoresystempickupcounter

Pickup Counter Script

I'm making a pickup system for my game, but my script isn't working. When i mover over the cube, it is destroyed but my money counter doesn't go up.

var moneydisp : GUIText; var money = 20;

function Update () { moneydisp.text = ""+ money; }

function OnTriggerEnter() { money = money++; Destroy(gameObject); }

what's going on here?

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

2 Replies

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

Answer by duck · May 17, 2010 at 04:52 PM

To increment an integer counter variable by 1, you just need to call the ++ operator, like this:

money++;

The reason for the strange behaviour in your script is that although the ++ operator increments the variable, it actually returns a different value - the value of the variable before it was incremented.

This means your variable gets incremented, but because you are putting the returned value back into the same variable, it gets immediately replaced with its original value.

An example, for clarification:

var a = 0; var b = 5;

a = b++;

print(a); // result is 5 (the original value of 'b', as returned by ++) print(b); // result is 6 (because b was incremented by the ++)

If you want to increment a variable by a value other than 1, you can use the similar += operator?

a += 4;  // increment 'a' by 4

Hope this helps clarify things!

In addition, there seems to be some confusion about which object does the counting, and displaying of the money sum. The script that detects the collection of the money & adding should be on your player, rather than on the money object. Make sure your money objects are tagged "Money", then place this script on your player:

var moneyDisplay : GUIText; var money = 20;

function Start() { DisplayAmount(); }

function DisplayAmount () { moneydisp.text = ""+ money; }

function OnTriggerEnter(other : Collider) { if (other.CompareTag("Money")) { money++; DisplayAmount(); Destroy(other.gameObject); } }

Remember to make sure that:

  • all money objects are tagged "Money"
  • all money objects have a trigger collider
  • the player object has either a CharacterController or rigidbody & collider
  • the "moneyDisplay" variable reference is linked up in the inspector

So, the above script (which is placed on the player) detects if the player touches a "Money" object. This single script counts the money total, and is responsible for displaying the amount, and for destroying the money object.

The money objects themselves need no script attached at all.

Comment
Add comment · Show 7 · 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 HolBol · May 17, 2010 at 05:08 PM 0
Share

okt tried this and it hasn't worked. Something wrong in my script?

var moneydisp : GUIText; var printedmoney = 0; var money = 20;

printedmoney = money++; function Update () {

moneydisp.text = printedmoney +"Pounds";

}

function OnTriggerEnter() {

money = money + 20; Destroy(gameObject); }

avatar image Novodantis 1 · May 17, 2010 at 09:51 PM 0
Share

Code can be odd to read in a comment, but seems to me this is assigning "printedmoney" the value money(+1) at the start, then you are incrementing money on collection (but still displaying the original "printedmoney" value). The bit outside Update() is only run when creating the object.

I think your original script will work fine if you change the line "money = money++;" to "money++;" or "money += 1;" as Duck illustrates above.

avatar image duck ♦♦ · May 18, 2010 at 08:42 AM 0
Share

for some reason you've introduced another variable called "printedmoney". You don't need that. Just use the exact script you started with (in your question), but just change the line which is supposed to increase the money, to: money++;

avatar image HolBol · Sep 30, 2010 at 07:36 PM 0
Share

This still isn't working, lol, i changed the script to how you suggested and it still won't add. :/

avatar image duck ♦♦ · Oct 18, 2010 at 09:26 PM 0
Share

Hey, thanks for co$$anonymous$$g back after all this time to accept the answer :-) Since you mentioned it's still not working, I've added a more detailed solution in the post. hope it solves it for you!

Show more comments
avatar image
1

Answer by karl_ · May 17, 2010 at 09:17 PM

To make things a bit cleaner I like to add a tag to the gameobjects that are acquirable, though it is optional. In the case that you'd like to avoid that simply take out the 'if' statement in the beginning of the second function.

var moneydisp : GUIText; var money = 20;

function Update () { moneydisp.text = ""+ money; }

function OnTriggerEnter() { if(gameObject.tag == "money") {
Debug.Log("You have picked up money"); Destroy(gameObject); money++; } }

Comment
Add comment · Show 3 · 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 Novodantis 1 · May 17, 2010 at 09:53 PM 0
Share

This is a neat way for your player to interact with objects in different ways, without being too specific. A handy tip, thanks! =)

avatar image HolBol · May 18, 2010 at 06:54 AM 0
Share

this still won't add, the money still stays the same.

avatar image AMDAndrew · Aug 14, 2012 at 06:59 PM 0
Share

var moneydisp : GUIText; var money = 20; var quantity = 10; // $$anonymous$$oney in the box / cube

function Update () { moneydisp.text = ""+ money; }

function OnTriggerEnter() { if(gameObject.tag == "money") {
Debug.Log("You have picked up money"); money = money + quantity; Destroy(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

1 Person is following this question.

avatar image

Related Questions

Staring system, problem... 2 Answers

¿Score system with the new UI? 0 Answers

how to limit the counting of multiple trigger collisions 1 Answer

Kill Counter --> Score 2 Answers

Score count increase on hit 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