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 whitetiger120 · Feb 18, 2016 at 09:20 AM · gamescorescore systemtroubleshootingscoreboard

My score system doesn't display or work? Please help me!

I am making a game that has a scoring system but it doesn't work. I am trying to make it where if the enemy dies the player gets 10 points and so on. So, I made a GUIText to display the score and made a script with the following:

using UnityEngine; using System.Collections; using UnityEngine.UI;

public class Score : MonoBehaviour { public GUIText scoreText; private int score;

 void Start()
 {
     score = 0;
     Update();
 }

 public void AddScore(int newScoreValue)
 {
     score += newScoreValue;
     Update();
 }

 void Update()
 {
     scoreText.text = "Score: " + score;
 }

} and on the enemy side, I put the next following script so it can die when hit and to scores a point but it doesn't work.

using UnityEngine; using System.Collections;

public class Damagebycrash : MonoBehaviour { int health = 1; public int scoreValue; private Score score;

 void OnTriggerEnter2D(Collider2D collider)    {
     if(collider.tag == "Invaders"){
         score.AddScore(scoreValue);
     }

     Debug.Log("Trigger!!");

     health--;

     if (health <= 0) {
         Die();
     }
 }

 void Die()
 {
     Destroy(gameObject);
 }

} so yeah please help me?

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

· Add your reply
  • Sort: 
avatar image
0

Answer by bcaloe · Feb 18, 2016 at 04:32 PM

First of all, "void Update()" method is called at every frame automatically. So there isn't really need to call Update () in your Start () and AddScore (int value).

Since i am lacking detailed information as to whether what is happening and what is not, I will be relying on my assumption based on the evaluation of only the code you posted.

I assume that enemies will die when collision is triggered. Correct? But I don't think your Score variable (private Score score) is assigned to anything yet. Hence, score.AddScore(scoreValue); will not do anything.

Try:

 public static void AddScore(int newScoreValue) {
       score += newScoreValue;
 }

for your method in Score and you also need to make your score declaration to be "static int score"

 void OnTriggerEnter2D(Collider2D collider){
        if(collider.tag == "Invaders") {
             Score.AddScore(scoreValue);
       }
 }
 

for your enemies' script.

This way, all enemies' scripts will have access to static method from Score script without having to assign the Score class variable.

Hope this helps : )

Comment
Add comment · Show 3 · 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 whitetiger120 · Feb 18, 2016 at 07:11 PM 0
Share

thanks, this helps little but the Score.AddScore(scoreValue); is causing an error ( "An object reference is required to access non-static member `Score.AddScore(int)'" ) and yes, enemy dies when the collision is triggered and score appears but when the player kills an enemy the score doesn't change, can you tell why and if you need more info ask me so you can help me and Thanks for helping me

avatar image bcaloe whitetiger120 · Feb 19, 2016 at 05:01 PM 0
Share

Oh, I see why you are getting that error message. When you changed your AddScore method to be of type "static", you should also change the type of the variable being modified in that method to be of the type "static".

That will resolve the problem : )

avatar image whitetiger120 bcaloe · Feb 19, 2016 at 08:04 PM 0
Share

Ok sorry for telling you this but when I kill an enemy the score doesn't update or doesn't change like at first it says Score: and then when I start the game it changes to Score:0 which is good because that the guiText script (Score) works. But when player kills enemy it says still says 0 here's the script for enemy and I fix the error thank you:

public class Damagebycrash : $$anonymous$$onoBehaviour { int health = 1;

 static int newScoreValue;
 public Score score;


 void OnTriggerEnter2D(Collider2D collider)    {
     if (collider.tag == "Invader") {
         Score.AddScore(newScoreValue);
     }

     Debug.Log("Trigger!!");

     health--;

     if (health <= 0) {
         Die();

     }
 }

 void Die()
 {
     Destroy(gameObject);
 }

}

and here's the guiText script (Score)

public class Score : $$anonymous$$onoBehaviour { public GUIText scoreText; public static int score; static int newScore;

 void Start()
 {
     score = 0;
 }


 public static void AddScore(int newScoreValue)
 {
     score += newScoreValue;
 }

 void Update()
 {
     scoreText.text = "Score: " + score;
 }

}

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to add and track scoring in online multiplayer with UI? (Unet) 0 Answers

Anyway To Set A Specific Part Of Code To A Scene? 2 Answers

How to add one score every second to scoremanager c# 1 Answer

show score and keep score on screen until start new game? 0 Answers

How do I make a Score and Highscore thingie in game over screen 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