- Home /
Why is my kill method not running the score system method call?
I have used Debug.Log(""); to make sure the score value is actually a number and not just zero. It is the number which i assigned it. I have also put a Debug.Log(""); on the other side of the call to see if it actually increases score. The Debug never appears in console. Please help me as im new to unity and im trying to code without a tutorial but I often look up fixes.
Code for my Enemy health, also my update to kill is on line 31 and the call is on 34:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class EnemyHealthManager : MonoBehaviour { public float maxHealth; public float currentHealth;
private int killCounter;
public int scoreValue;
private int standerdCounter;
private int tankCounter;
private int fastCounter;
public Image healthImage;
// Start is called before the first frame update
void Start()
{
currentHealth = maxHealth;
fastCounter = 0;
standerdCounter = 0;
tankCounter = 0;
}
// Update is called once per frame
void Update()
{
if (currentHealth <= 0)
{
Destroy(gameObject);
ScoreSystem.score += scoreValue;
EmemyCheck();
killCounter += 1;
}
}
public void EmemyCheck()
{
if (maxHealth == 50)
{
standerdCounter += 1;
}
else if(maxHealth == 30)
{
fastCounter += 1;
}
else
{
tankCounter += 1;
}
}
public void HurtEnemy(int damage)
{
currentHealth -= damage;
healthImage.fillAmount = (currentHealth / maxHealth);
}
}
code fore my score system:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class ScoreSystem : MonoBehaviour { public static int score; Text scoreText;
// Start is called before the first frame update
void Awake()
{
scoreText = GetComponent<Text>();
score = 0;
}
// Update is called once per frame
public void Update()
{
scoreText.text = "Score: " + score;
}
}
PLEASE HELP I'M SO TIRED OF THIS. ALSO THERE ARE NO RUN-TIME OR COMPILE ERRORS!! However before I was getting a non-static error for my method call, but my variable that was being called was static so i assumed it just a bug and restarted my unity project. The error didn't show anymore but it still doesn't work. It could be the same issue but just not showing? thanks for reading.
Answer by sh_code · Jan 13, 2019 at 08:23 PM
How about you FIRST INCREASE THE SCORE, and THEN destroy self? ;)
Destroy is a weird one, you don't have a guarantee WHEN it will happen, whether immediately, or on the end of current frame, or sometime within the next few frames.
BUT, in any case, FIRST you want the object to do everything it needs to, and THEN you want it to say to the engine it wants to be destroyed.
TL;DR : move the Destroy call so that it's the LAST LINE of the function. ALWAYS.
Your answer
![](https://koobas.hobune.stream/wayback/20220612185648im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
C# Going Static or Going OOP 3 Answers
Using a static class & pass in gameObject or just add same script to multiple objects 1 Answer
Scoring help ! 1 Answer
Trying to run 5 instances of a static method at once to do an action, probably a dumb thing to do? 0 Answers
An object reference is required to accsess a non-static member 'Lives.loselife()' 1 Answer