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 /
avatar image
0
Question by Drakkwyr · Aug 06, 2019 at 05:51 PM · scorescore systemscoreboard

Score not increasing after killing 1 enemy

Im trying to make a score system that increases each time a enemy dies, but instead it adds 1 time points and then it stays on 100, what might be the problem here?

 private void OnCollisionEnter(Collision coll)
     {
         if (coll.gameObject.tag == "Bullet")
         {
             Death();
         }
     }
 
     void Death()
     {
         Instantiate(DeathEffect, transform.position, transform.rotation);
         Destroy(this.gameObject);
         points = points + 100;
         Text();
     }
 
     void Text()
     {
         score.text = "Score:" + points;
     }
 }

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

2 Replies

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

Answer by k234234w · Aug 06, 2019 at 06:03 PM

If points is a local variable on that script, every time an enemy collides with a bullet they will update the score text to 100 (and never past 100). The enemies do not know that points has been updated because they each have their own points variable. You could have a ScoreManager in the scene with its own points variable and every time an enemy collides with a bullet you tell the ScoreManager to update its points, and then set the text to the new value.

Comment
Add comment · 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
0

Answer by QaunainM · Oct 19, 2021 at 04:09 PM

The reason this normally happens is because you are destroying the GameObject which has the script attached to it and that script is the element incrementing and storing the score variable but as it's getting destroyed it's can't update the score counter.

One solution to solve this is to use Callback Actions.

Assume you have a Bullet (which hits and kills enemies) and a Player which launches the bullet

In your Bullet script: Add this namespace at the top so you can use callbacks using System;

Then at the top of the Bullet script just above void Start() {} add this

public Action onHitEnemy;` //this is used to receive an action when the bullet hits an enemy

Then inside the same bullet script at the end of your OnTriggerEnter (Collider other) code add

 if (onHitEnemy != null)  {
                 onHitEnemy(); 
     }

Now inside your Player script

You need to also add the same namespace at the top so you can use callbacks using System;

Then add a variable for the score like this public int score = 0;

Then wherever you instantiate your bullet, inside that same block (i.e. inside the if block that detects the user's button input to shoot the bullet) add this:

    Bullet bullet = bulletObject.GetComponent<Bullet>();     
             bullet.onHitEnemy = () => {
                 score++; 
             };
     
 //bulletObject is referring to the variable I have already created in this script which I use to instantiate and addforce to the bullet prefab.


The result of adding these new callback actions in the two different C# files for the Bullet and Player is that:

  • Every time your bullet fires (instantiated from the Player inside its Update method) it's looking to see if the onHitEnemy action is true.

  • When the Bullet detects a collision with the enemy via the OnTriggerEnter method it adds an action reference to the onHitEnemy method

  • When the two points above match it then means that score++; (aka score = score + 1;) can take place and this is stored inside the Player script so it doesn't matter if the bullet GameObject gets destroyed.

Comment
Add comment · 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

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

112 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

Related Questions

my code is suppose to store high score and its storing recent score insted and it wont stop if game over and it has an score counter as well plzz help 0 Answers

I need my script to load and save my players score to a text file . When I attach the script to a gameobject the script either is not found or it just prints 5,4,3,2,1 or all )0's. 0 Answers

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 save score for survival time? 1 Answer

How to create a HighScore using playerprefs based on passed time? 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