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 /
This question was closed Oct 29, 2015 at 01:36 PM by huzzuh for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by huzzuh · Oct 29, 2015 at 01:24 AM · c#scorescore systemscores

Need Help with Dual Scoring System C# (score over time and kill score)

Hi, relatively new here and also with C# / Unity.

I'm building a game where the scoring is based on how long the player survives (score increases over time) but the player can also kill enemies to add on to the score (bonus score for each kill).

I use 2 scripts, one on my camera which updates the score over time and the other on my enemy which is supposed to update the bonus score. I can't seem to get the bonus score to update even though everything else is working fine.

Here's the first script on my Camera.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class ScoreBehaviour : MonoBehaviour
 {
     public static float score;
     public Text scoreText;
 
     void Start()
     {
         score = 0;
     }
 
     void Update()
     {
         score = Time.timeSinceLevelLoad * 27;
         int showscore = (int)score;
         scoreText.text = "Score: " + showscore.ToString ();
     }
 
     public void KillScore()
     {    
         score = (Time.timeSinceLevelLoad * 27) + 500;
         int showscore = (int)score;
         scoreText.text = "Score: " + showscore.ToString ();
     }
 }

And the second script on my Enemy.

 using UnityEngine;
 using System.Collections;
 
 public class EnemyBehaviour : MonoBehaviour
 {
     private ScoreBehaviour scrScript;
     float speed = 5f;
         
     void Start() 
     {
         scrScript = GameObject.Find ("MainCamera").GetComponent<ScoreBehaviour> ();
     }
     
     void Awake()
     {
         Destroy(gameObject, 15f);
     }
 
     void Update ()
     {
         transform.Translate(0, 0, speed * Time.deltaTime);
     }
 
     void OnTriggerEnter(Collider laser)
     {
         if (laser.gameObject.tag == "Laser")
         {
             scrScript.KillScore();
             laser.gameObject.GetComponent<MeshRenderer>().enabled = false;
             Destroy (laser.gameObject, 0.7f);
             Destroy(this.gameObject);
         }
     }
 }
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

1 Reply

  • Sort: 
avatar image
2
Best Answer

Answer by OncaLupe · Oct 29, 2015 at 04:09 AM

The problem is you're directly setting the variable score in the Update method, so no matter what change is made to it, the next frame it gets set back. Instead you'll want to increment it with something like this:

score = score + (Time.deltaTime * 27);

or a slightly shorter but equivalent style:

score += Time.deltaTime * 27;

Time.deltaTime is how long the frame took to perform, so adding that every frame will basically do the same thing as your code, but allow it to be changed in other sections of code. Then in the KillScore method:

score += 500;

This will just add 500 to whatever is currently in the score variable, regardless of how long the level has been running.

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 huzzuh · Oct 29, 2015 at 01:35 PM 0
Share

Ahh.. that was a silly mistake. Thank you so much!

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to check if variable is equal to range of integers 2 Answers

Scoring Points. 1 Answer

how to creat and scoring and high score system for an endless runner,how do I display my score on a game over scene and also create and high score system for the same ? 0 Answers

How to add score after destroying object in unity 2D? 1 Answer

Add bonus to score 1 Answer


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