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 dutarus · May 23, 2021 at 03:56 AM · scoreissues

Score is not increasing after one game object is destroyed

So, in my game I have the score UI working but it only changes once and when my player kills another enemy it will change it to the enemy's score points but it doesn't add on to the total score or existing score. I have done a lot of researching but I cannot seem to find a solution to this issue. As an example score UI says 0 at the start of the game and when I destroy the enemy game object it posts the score points as 5 now when I defeat another game object it will display its reward scoring points 6 but it won't add on to the current score of 5 making it 11. It just remains at 6 points. Is there a way to fix this issue? I have attached my code on this text file and pasted it on this forum if anyone wants to view it.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.AI;
 using UnityEngine.UI;
 using TMPro; 
 public class EnemyController : MonoBehaviour
 {
     [Header("Unity Setup")]
     public ParticleSystem deathParticle;
     
     [SerializeField]
     Transform _destination;
 
     NavMeshAgent _navMeshAgent;
    
     public int hitPoints = 10;
 
     private int scorePoints;
     public int scoreValue;
     
     public int seconds = 1;
 
     bool isforceFieldActive;
     bool isScoreAdding;
     
     bool ifDead;
     
     public TextMeshProUGUI ScoreDisplay;
 
 
     // Start is called before the first frame update
     void Start()
     {
         scorePoints = 0;
         UpdateScore(0);
         _navMeshAgent = this.GetComponent<NavMeshAgent>();
         
         if (_navMeshAgent == null)
         {
             Debug.LogError("The nav mesh agent component isn't attached to  " + gameObject.name);
         }
 
         else
         {
             SetDestination();
         }
         ScoreDisplay.text = "Score:" + scorePoints.ToString();       
     }
 
     void Update()
     {        
         DestroyEnemy();                                             
     }
 
     //EnemyMovement
     private void SetDestination()
     {
         Vector3 targetVector = _destination.transform.position;
         _navMeshAgent.SetDestination(targetVector);
     }
 
     //Destroy the Enemy 
     public void DestroyEnemy()
     {
         if (hitPoints <= 0)
         {                   
             deathParticle = Instantiate(deathParticle, transform.position, Quaternion.identity) as ParticleSystem;
             
             StartCoroutine(Autodestroy());                      
             Destroy(deathParticle, 4.0f);
             
             ScoreDisplay.text = "Score:" + scorePoints.ToString();
             Destroy(this.gameObject);           
             ifDead = true;                      
         }
     }
     
     IEnumerator Autodestroy()
     {
         UpdateScore(scoreValue);
         yield return new WaitForSeconds(deathParticle.main.duration);
        
     }
  
     private void UpdateScore(int scoreIncrease)  
     {
         scorePoints += scoreIncrease;                  
     }
 
     void OnTriggerEnter(Collider other)
     {        
         if (other.tag == "Player")
         {           
             Debug.Log("Damage Success!");           
             other.gameObject.GetComponent<PlayerController>().lives -= 1;                       
         }
        
     }
     
 
 }






code.txt (3.0 kB)
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by rh_galaxy · May 23, 2021 at 12:00 PM

When you do

 StartCoroutine(Autodestroy());

That will run Autodestroy() later (next frame or maybe later this frame), but it won't have the chance to run before you display the text...

Why have this in a coroutine? Can't you just update the score immediately?

 scorePoints += scoreValue; //set scoreValue to 6 before this, if that is what you want.

instead of

 StartCoroutine(Autodestroy());

Problem solved... Don't use coroutines if it's not necessary. Second row in your corourtine will accomplish nothing.

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 dutarus · May 23, 2021 at 01:39 PM

I tried it before and I was still getting the same results i changed the variable from scoreIncrease to scoreValue. It's still not adding on to the existing score. is this what you meant?

  void Update()
     {        
         DestroyEnemy();
         if (ifDead == true)
         {
             scorePoints += scoreValue;
             ScoreDisplay.text = "Score:" + scorePoints.ToString();
         }
        
     }
 
 public void DestroyEnemy()
     {
         if (hitPoints <= 0)
         {                   
             deathParticle = Instantiate(deathParticle, transform.position, Quaternion.identity) as ParticleSystem;
                              
             Destroy(deathParticle, 4.0f);
             
             Destroy(this.gameObject);           
             ifDead = true;                      
         }
     }
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 rh_galaxy · May 23, 2021 at 02:40 PM 0
Share

When you make a coroutine run, it doesn't run directly, you queue it up for later, and then it runs once every frame until it is done.

I meant this:

 void Update()
 {        
      DestroyEnemy();
 }
 public void DestroyEnemy()
 {
     if (hitPoints <= 0)
     {                   
         deathParticle = Instantiate(deathParticle, transform.position, Quaternion.identity) as ParticleSystem;
         scorePoints += scoreValue; //<- this row is replacing the old, set scoreValue before this runs
         Destroy(deathParticle, 4.0f);
 
         ScoreDisplay.text = "Score:" + scorePoints.ToString();
         Destroy(this.gameObject);           
         ifDead = true;                      
     }
 }

And then you can delete Autodestroy() and UpdateScore().

avatar image
0

Answer by QaunainM · Oct 19, 2021 at 04:04 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

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

Related Questions

Keeping a score from one scene to another? 2 Answers

Play Sound on Score 2 Answers

High score in standalone 1 Answer

PlayerPrefs Highscore problem 1 Answer

can xml be used in anrdoid?? 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