- Home /
[JS] I'm having troubles with my money system
So I made a working basic money system where there is the variable CurrentMoney and the variable MoneyAmount, and basically every time I walk over money, the variable CurrentMoney is increased by 50, and then the game object is deleted. Now the problem with this, is when the game object is deleted, so is the variable CurrentMoney, because the money script is located UNDER the object "money" ... Ive tried a lot to solve this and I just can't think of what to do... Does anybody else know how I could make it so that the variables don't get erased???
Answer by thepenguinmaster · Dec 01, 2014 at 09:14 PM
What you should do, is CurrentMoney should be attached to a character. When you hit the money, the character (what you control) should have the money added. You might even in some cases have a static class that holds a player reference which contains the money. This would be a good case if you had to remove the player for some reasons and didnt want the money attached to the player.
The best way to think about this, is your wallet holds money, not the money you pick up. You hold your wallet, so you being the player, who never goes away, should hold the wallet (and the picked up money) or the money count.
I think the problem is more how you structured things. You might be representing the plyer as the camera, but even then shoudl attach to a gameobject hat also has a script with your player settings.
Im getting an error. So under the object money I have this script
#pragma strict
var $$anonymous$$oneyAmount : int = 50;
function OnTriggerEnter (col : Collider) {
if (col.gameObject.tag == "Player") {
Current$$anonymous$$oney = Current$$anonymous$$oney + $$anonymous$$oneyAmount;
Debug.Log("Contact was made");
Destroy (gameObject);
}
}
and then under my first person controller I have this script called PlayerStats
#pragma strict
var Current$$anonymous$$oney : int;
SO I'm getting an error that says "$$anonymous$$ Identifier: Current$$anonymous$$oney" I have no idea what that means ._.
This still has not been resolved if anybody is wondering. I replied to ThePenguin$$anonymous$$aster the problem im having :/
You need to find and store your reference to the script, that has the var "Current$$anonymous$$oney" in it, in your script that uses it. Look at this part of the docs :
https://docs.unity3d.com/352/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
I recently got Unity so I dont really understand that :/
Ok the problem here is that Current$$anonymous$$oney exists in the player, and the way you are doing it trys to reference the variable in the money script.
Your problem is a problem with what is known as 'scope'.
This means that player can have Current$$anonymous$$oney, and optionally you can define Current$$anonymous$$oney in the money object as well, but they are not the same.
To access an objects variable, you need to use myObject.Current$$anonymous$$oney. In your case, you have PlayerSTats.Current$$anonymous$$oney, but are trying to set the value to $$anonymous$$oney.Current$$anonymous$$oney. Since you did not declare Current$$anonymous$$oney in the $$anonymous$$oney script (which you dont want to do anyways) it is likely throwing an object reference error of some kind.
So here is the solution, on the collision function, you need to get your script from the col parameter and set the Current$$anonymous$$oney there.
Example:
#pragma strict
var $$anonymous$$oneyAmount : int = 50;
function OnTriggerEnter (col : Collider) {
var player = col.gameObject;
if (player.tag == "Player") {
PlayerStats stats = player.GetComponent(PlayerStats);
stats.Current$$anonymous$$oney = stats.Current$$anonymous$$oney + $$anonymous$$oneyAmount;
Debug.Log("Contact was made");
Destroy (gameObject);
}
}
Something kind of like that. I dont do much javascript so I didnt test the code, but you should be able to get the idea from here. Also a link:
http://answers.unity3d.com/questions/575974/how-to-access-a-colliders-function-or-variable.html
Here is a google search phrase that will address your problem: "unity javascript on trigger call function in other"
Also I would recommend looking into 'scope' in relation to program$$anonymous$$g. It will help you in the future.
Answer by Unitraxx · Dec 01, 2014 at 08:56 PM
The simplest solution is to add the variable to your player object and not to a money object.
Your answer
Follow this Question
Related Questions
need some help with this things 0 Answers
collectable currency doesn't transfer between different scenes 0 Answers
how to connect our money on the bank with money on our game?? 1 Answer
Revenue exceeds personal limit 1 Answer
Adding A Script to an Instantiated Game Object - And Adding Values - 2020 3 Answers