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
0
Question by Jonathan07 · Feb 21, 2014 at 01:22 AM · scoreintincreased

Increasing an int everytime another int reaches an amount.

Hi, I'm new to Unity3d and I'm working on my first project to get to know the software and learn C#. I'm trying to make an upgrade system with points. I have a score variable (int) which is increased by 50 every time you kill an enemy and each time you have 1000 score I would like another variable (int uPoints) to be increased by 1.

The tricky part here is that when you die 3 times you are game over so score resets but i wan uPoints to keep it's value. Anyone can help me with this please? Here's my score script, it just need the uPoints line(s) of code:

 using UnityEngine;
 using System.Collections;
 
 public class Score : MonoBehaviour {
 
     public static int score = 0;
     public static int uPoints = 0;
 
     void OnTriggerEnter(Collider collider){
         if(collider.gameObject.CompareTag ("Enemy")){
             score += 50;
             Destroy(this.gameObject);
         }
     }
 }

Thanks!

Comment
Add comment · Show 2
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 rutter · Feb 21, 2014 at 01:54 AM 0
Share

Since those are static fields, they will keep whatever value you give them (until the game exits). You'd have to reset them to zero manually, whenever you want that to happen (for example, when the player dies X times, or possibly when changing levels or buying upgrades).

If you have code that kills the player, this reset logic probably goes in there.

avatar image Jonathan07 · Feb 21, 2014 at 02:23 AM 0
Share

Well, my score is being reset when the player is game over with the following script:

 using UnityEngine;
 using System.Collections;
 
 public class gameOver : $$anonymous$$onoBehaviour {
 
     public static bool isGameOver = false;
     
     // Update is called once per frame
     void Update () {
     
         if (Lives.playerLives < 0) {
 
             isGameOver = true;
             Destroy(GameObject.FindWithTag("Enemy"));
             Invoke("Reload", 3);
                 
         }
 
     }
 
     void OnGUI(){
 
         if (isGameOver == true) {
                         GUI.Box (new Rect (Screen.width / 2 - 40, Screen.height / 2 - 10, 80, 20), "Game Over!");
                 }
     }
     
         void Reload(){
         
             Lives.playerAlive = true;
             Score.score = 0;
             gameOver.isGameOver = false;
             Lives.playerLives = 3;
             enemySpawn.enemyNumber = 0;
             Application.LoadLevel("Hud");
     
         }
     }

The only way I know of that would make uPoints be increased by one every 100 score is by doing

public int uPoints { get{ return score / 750; }}

but as soon as score would be reset, so would uPoints.

1 Reply

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

Answer by mattyman174 · Feb 21, 2014 at 02:35 AM

Use the Modulo Operator (%)

http://msdn.microsoft.com/en-us/library/0w4e0fzs.aspx

 if((score % 100) == 0 && score != 0)
 {
     uPoints++;
 }

Modulo gives the remainder after a division.

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 Jonathan07 · Feb 21, 2014 at 02:58 AM 0
Share

Will try! Seems perfect since it doesn't depend on the score var to have a value itself. Thanks a lot! Would rep you but I can't yet

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

21 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

Related Questions

How do i link a GUI text to a variable text which is a integer? 2 Answers

Multiply float by int? 2 Answers

Adding coin score to multiplied value 2 Answers

Check if a guiText = a number? 2 Answers

int +=1 won't exceed 1 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