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 Kman43759 · Dec 01, 2014 at 08:54 PM · moneysystem

[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???

Comment
Add comment · Show 1
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 Kman43759 · Dec 02, 2014 at 02:13 AM 0
Share

Would somebody PLEASE help

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 5 · 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 Kman43759 · Dec 01, 2014 at 09:38 PM 0
Share

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 ._.

avatar image Kman43759 · Dec 01, 2014 at 09:59 PM -1
Share

This still has not been resolved if anybody is wondering. I replied to ThePenguin$$anonymous$$aster the problem im having :/

avatar image MrSoad Kman43759 · Dec 01, 2014 at 10:15 PM 0
Share

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

avatar image Kman43759 Kman43759 · Dec 01, 2014 at 11:04 PM 0
Share

I recently got Unity so I dont really understand that :/

avatar image thepenguinmaster · Dec 02, 2014 at 01:42 PM 0
Share

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.

avatar image
0

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.

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 Kman43759 · Dec 01, 2014 at 08:58 PM 0
Share

I'll try this and get back to you! 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

27 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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