|
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.
what's going on here?
(comments are locked)
|
|
To increment an integer counter variable by 1, you just need to call the ++ operator, like this:
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:
If you want to increment a variable by a value other than 1, you can use the similar += operator?
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:
Remember to make sure that:
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. 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); }
May 17 '10 at 05:08 PM
Fishman92
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.
May 17 '10 at 09:51 PM
Novodantis 1
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++;
May 18 '10 at 08:42 AM
duck ♦♦
This still isn't working, lol, i changed the script to how you suggested and it still won't add. :/
Sep 30 '10 at 07:36 PM
Fishman92
Hey, thanks for coming 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!
Oct 18 '10 at 09:26 PM
duck ♦♦
(comments are locked)
|
|
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. This is a neat way for your player to interact with objects in different ways, without being too specific. A handy tip, thanks! =)
May 17 '10 at 09:53 PM
Novodantis 1
this still won't add, the money still stays the same.
May 18 '10 at 06:54 AM
Fishman92
var moneydisp : GUIText; var money = 20; var quantity = 10; // Money in the box / cube function Update () { moneydisp.text = ""+ money; } function OnTriggerEnter() { if(gameObject.tag == "money") { }
Aug 14 '12 at 06:59 PM
AMDAndrew
(comments are locked)
|
