- Home /
Basic scoring not working
hi everyone, im trying to do a wave system with a score value, enemy dies increment 1 to the wavecounter and when the wave counter is equal to the wavelimit start next wave(which i havent created but thats another topic). Basically saying when the wave limit is reached start next wave.
Here´s the enemy code(the place where im trying to implement) :
public class Enemy : MonoBehaviour {
public float EnemySpeed;
public float EnemyStoppingDistance;
public float EnemyReatreatDistance;
private float TimeBetweenShots;
public float StartTimeBetweenShots;
public int Health;
public GameObject Projectile;
private Transform Target;
public GameObject DeathEffect;
private GameObject objSpawn;
public int WaveLimit;
public int WaveCounter;
void Start()
{
Target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
TimeBetweenShots = StartTimeBetweenShots;
objSpawn = (GameObject)GameObject.FindWithTag("Spawner");
}
void Update()
{
if (Vector2.Distance(transform.position, Target.position) > EnemyStoppingDistance)
{
transform.position = Vector2.MoveTowards(transform.position, Target.position, EnemySpeed * Time.deltaTime);
}
else if (Vector2.Distance(transform.position, Target.position) < EnemyStoppingDistance && Vector2.Distance(transform.position, Target.position) > EnemyReatreatDistance)
{
transform.position = this.transform.position;
}
else if(Vector2.Distance(transform.position, Target.position) < EnemyReatreatDistance)
{
transform.position=Vector2.MoveTowards(transform.position, Target.position, -EnemySpeed * Time.deltaTime);
}
if (TimeBetweenShots <= 0)
{
Instantiate(Projectile, transform.position, Quaternion.identity);
TimeBetweenShots = StartTimeBetweenShots;
}
else
{
TimeBetweenShots -= Time.deltaTime;
}
if(Health<= 0)
{
Instantiate(DeathEffect, transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
public void OnDestroy()
{
WaveCounter++;
if (WaveCounter == WaveLimit)
{
Debug.Log("WaveLimit reached");
}
}
public void TakeDamage(int Damage)
{
Health -= Damage;
}
}
the exact place where the score system is is:
public void OnDestroy()
{
WaveCounter++;
if (WaveCounter == WaveLimit)
{
Debug.Log("WaveLimit reached");
}
} This log i put isnt appearing thats why i detected a problem.
Answer by I_Am_Err00r · Jul 12, 2019 at 02:57 PM
I don't see where you are calling OnDestroy, Destroy(gameObject) is a Unity function and will not call OnDestroy for you.
Try this:
if(Health<= 0)
{
OnDestroy();
Instantiate(DeathEffect, transform.position, Quaternion.identity);
Destroy(gameObject);
}
I don't know if how the waves progress this is where you would want to call OnDestroy, but you need to call that function yourself.
Let me know if that is the problem.
Answer by pedromrnosorio97 · Jul 12, 2019 at 04:43 PM
the message is showing but only thing is that the counter number doesnt go up and if i try to set the limit to 2 or 3 it shows even if i didnt reach the limit, thanks for responding im very new at unity and programming so im very apreciative of that.
I'm confused by what you are saying here.
Give me step by step what it is doing, and then step by step what you need it to do.
what is happening is that when i kill an enemy it shows the message. what i wanted to happen is the message to be shown when the wavecounter is equal to the wave limit in this case when i set the wavelimit to 3 i want to kill 3 enemies and the message would be showned.
Did you setup the waveLimit in the inspector? It's set to public, so you can either hardcode it in the script at start or something, or just put 3 in the spot in the inspector for waveLimit.
yes i putted the value on the ispector thats the part that is bugging me
Debug.log is the next step here if you are certain that the wavelimit is set.
void Start()
{
Target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
TimeBetweenShots = StartTimeBetweenShots;
objSpawn = (GameObject)GameObject.FindWithTag("Spawner");
Debug.log(WaveLimit);
}
and then again here to make sure they match:
WaveCounter++;
Debug.log(WaveLimit);
if (WaveCounter == WaveLimit)
{
Debug.Log("WaveLimit reached");
}
And just to be safe, let's add a log here too, and maybe move the OnDestroy() line towards the bottom of that:
if(Health<= 0)
{
Debug.log("The health just reached zero, is this appearing more than once per enemy?");
Instantiate(DeathEffect, transform.position, Quaternion.identity);
Destroy(gameObject);
OnDestroy();
}
If everything is setup correctly, you should get a log at start clarifying with the WaveLimit is, if it says anything but what you put in the inspector or if it is correct at start and somehow changes when you kill an enemy, then you have another script somewhere that is accessing that script and changing the value;
sorry for the late response i was way for the weekend. I did what you said and what happened was this: -At start it shows the wave limit, 2 in this case -When i kill 1 enemy it shows the health has reached zero message -Then appears 2 more messages with 2, which i think is the wave limit -Then appears the message Wave limit reached also -Everytime an enemy spawns in it shows the message "2" whic again i think is the wavelimit
Perhaps the problem is the fact that i putted in my enemy script?
Your answer
Follow this Question
Related Questions
score display not showing 1 Answer
Unity 2017.1.0f3 Failing To Start 0 Answers
Latest SDK not working 0 Answers
Issues with script in unity 5.4.5p5 and VS 2017 1 Answer
UI Text not changing color [C#] 2 Answers