Int wont increase
Hi there, I'm a new programmer and I have a question about my code! I want to set it so that my bool goes from true to false whenever my enemies hit a certain amount that have been initiated. The problem is my int of enemyCount is just not incrementing. Any Idea why? P.S don't be afraid to criticize anything else about my code as I'm still new and want to improve.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyDeath : MonoBehaviour
{
[SerializeField] private GameObject player;
[SerializeField] private GameObject deathTrigger;
[SerializeField] private ParticleSystem enemyGhostDeathFX;
[SerializeField] private float deathDelay;
[SerializeField] private int enemyLimit;
private int _enemyCount;
private bool _giveCoin;
//TODO Stop player from giving coin and spawning enemies when the limit of enemies is hit
void Start()
{
_enemyCount = 0;
_giveCoin = true;
player = GameObject.Find("Player");
}
private void Update()
{
if (_enemyCount > enemyLimit)
{
_giveCoin = false;
}
}
private void OnTriggerEnter(Collider other)
{
if(other.CompareTag("Player"))
{
deathTrigger.SetActive(false);
EnemyDeathSequence();
}
}
private void AddEnemyCoin()
{
player.GetComponent<EnemyCoin>().enemyCoinAdd();
}
private void EnemyDeathSequence()
{
enemyGhostDeathFX.Play();
Destroy(gameObject, deathDelay);
EnemyRespawn();
RingAbilityAdd();
print(_giveCoin);
CheckToGiveCoin();
}
private void CheckToGiveCoin()
{
if (_giveCoin == true)
{
AddEnemyCoin();
}
else if (_giveCoin == false)
{
}
}
public void EnemyRespawn()
{
_enemyCount++;
Instantiate(PrefabManager.Instance.EnemyPrefab);
print(_enemyCount);
}
private void RingAbilityAdd()
{
player.GetComponent<Ability>().AddRingAbility();
}
}
you might want to initialize the _enemycount int to 0 (or what is appropriate).
you might want to add a debug statement before the "_enemyCount += 1" to check if it's a problem with that part of the code.
if not, just propagate outwards with debug statements until you get to the problematic line.
also, could it be that the initiated components change this value?
That's actually not a bad debugging method for this issue. I actually ended up solving the issue with another users idea but Ill keep your idea in $$anonymous$$d next time I run into a similar bug.
Answer by tadadosi · May 31, 2020 at 06:37 PM
With a quick look it seems like you are destroying the gameobject attached to EnemyDeath
who is also the one that should increase _enemyCount, after you destroy it, that count is gone and it will just start from zero
. Maybe you should think about adding a score manager with a static count for your enemies or a script that manages your enemies and keeps the count without being destroyed.
This fixed my problem. This script was meant to just act as EnemyDeath but ins$$anonymous$$d starting acting like a manager. I added a new GameObject in my level and applied a EnemyCount$$anonymous$$anager to keep track of the amount enemies In my level like I needed to.
Answer by unity_ltNnNYF1WDuhbA · May 31, 2020 at 11:28 AM
I think the problem is that your EnemyDeath function is not called and it happens because you are not checking who is Enter on Trigger: You should specify who is on Trigger Enter. Give a tag to your player and then check it like: if(other.tag == "tagthatyougivetoplayer") the do stuff.
in your case it's not other.tag, but collide.tag i guess, anyway, better search for it =))
Didn't fix the problem, however you are right that I need to specify who's on the trigger enter. I went ahead and changed that.
Your answer
Follow this Question
Related Questions
Dedicated server for unity game 0 Answers
How can I have a life added bar every time I reach 5000 points 0 Answers
Map creator inside my game 0 Answers
The location of the object by points 0 Answers