Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by wasicool7 · Oct 18, 2017 at 01:01 PM · random.rangecoin

Unity2D: Fixing Coin System

The way my coin system works is simple.

  1. The player starts off with 10 fruit automatically

  2. The enemies will then spawn in and steal a random amount of fruit from the player, the amount of fruit the enemy will steal varies from a random integer to the amount the player previously had.

  3. The player can get more fruit by stabbing the enemies, the amount of fruit the player will get varies from a random integer to random integer.

  4. However if the enemy takes a random amount of fruit from the player and then the player then stabs the enemy, the player will get back however much fruit the enemy took from them, plus the an extra bonus of fruit from the enemy (please look at point 3, if needed).

My problem:

This only happens occasionally, but sometimes the coin system will refuse to work especially during the beginning of the game or if the player has a large amount of fruit, i.e. 300 fruits. For example, if the player has about 300 fruits and the enemy takes about 200 fruits from the player (meaning the player will have 100 fruits remanding), then if the player stabs the enemy, the player will only receive about (for example) 25 fruits back instead of the initial 200 + the bonus(for example) 25. As mentioned before, this only happen occasionally - sometimes more than usual however I'd still like to make sure that it works accurately and smoothly. I have tried debugging it, but I am still stuck with why it is not working properly. Can anyone help me fix my codes or give me a better solution as to how I can make a better coin system. Thank you!

Enemy Theft script:

 public static int scoreValue;

 void Start () {
     scoreValue = 0;
 }

 void Update () {
     Debug.Log ("ScoreValue:  " + scoreValue);
 }

 void OnTriggerEnter2D(Collider2D col)
 {
     if (col.tag == "Farm") {
         collidedwithFarm = true;
         scoreValue = Random.Range (1, GameController.Fruit);
         Debug.Log ("Enemy takes:  " + scoreValue);
         GameController.Fruit-= scoreValue;
         //Wallet += scoreValue;
         if (GameController.Fruit<= 0) {
             GameController.Fruit = 0;
         }
     }
 }


Enemy's script:

 public int randomValue;
 public int wallet;

 public bool collidedwithFarm = false;

 // Use this for initialization
 void Start () {
     wallet = 0;
     collidedwithFarm = false;
     randomValue = Random.Range (1, 101);

 }

 void OnTriggerEnter2D(Collider2D col)
 {
     if (col.tag == "playerKnife") {
         if (collidedwithFarm == false) {
             text.text = " " + randomValue;
             Debug.Log ("NOT COLLISION");
             GameController.Fruit+= randomValue;
         }
         else if (collidedwithFarm == true) {
             Debug.Log ("COLLISION");
             GameController.Fruit+= EnemyTheft.scoreValue+ randomValue;                            
             Debug.Log("Player gets back: " + EnemyTheft.scoreValue);
             wallet += EnemyTheft.scoreValue + randomValue;    
             text.text = " " + wallet;
             Debug.Log ("In total:  " + wallet);
         }
     }
 }

Coin Script:

 public Text FruitText;
 public static int Fruit = 10; 

 void Start()
 {
     SetFruitText ();
     Fruit = 10; 
 }


 void SetFruitText ()
 {
     FruitText.text = "Fruits: " + Fruit.ToString();
 }
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

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

Coins are not spawning but the life is spawning? 0 Answers

Random.Range not working? 2 Answers

Generate Random Numbers a Distance Apart From Each Other 1 Answer

Random.Range method "left-hand side" error 2 Answers

Problem with random Range, returns always same number 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