Hi everyone,
I’ve been breaking my neck on this problem for a few hours now and since I can’t find any info that helps me anywhere apparently, I’ve come here to ask for some help. I know what the issue is, I think, but I dont know how to work around it or solve it.
So I have an emptyobject (Ill call it weapon) with a trigger on it, and when it collides with an object tagged as “enemy”, the weapon’s script sets a boolean in the script of the enemy it’s killing, “dead”, to true. This is just some background.
When dead becomes true, it sets the Enemy-object to inactive. After this a cooldown comes up (supposedly), after which the enemy becomes active again. (As a form of respawning.) Herein lies the problem (I think): The parent cant activate the inactive child, because the code of the child cant be reached BECAUSE the child is inactive… like a vicious cycle?
Please, if I got this all wrong, do explain it to me! Im really eager to learn and I’ve only gotten started like a week ago. Thanks for your time in advance!
The code of the EnemyController, the script on the Enemy itsself:
public class EnemyController : MonoBehaviour {
public float knockBackX, knockBackXneg, knockBackY, stunTime;
public bool dead;
public bool activated;
public GameObject enemyPrefab;
public float currentcooldown, recooldown;
void Start () {
dead = false;
activated = true;
gameObject.SetActive (true);
}
void Update () {
if (dead == true){
activated = false;
gameObject.SetActive (false);
}
}
}
The code of the parent of the “enemy” object:
public class enemyParent : MonoBehaviour {
public int currentcountdown;
void Start (){
}
void FixedUpdate () {
if ((GetComponentInChildren<EnemyController> ().activated == false) && (currentcountdown != 20)) {
currentcountdown += 1;
} else if ((GetComponentInChildren<EnemyController> ().activated == false) && (currentcountdown == 20)) {
GetComponentInChildren<EnemyController> ().dead = false;
GetComponentInChildren<EnemyController> ().gameObject.SetActive(true);
}
}
}
Oh and last but not least, when I destroy the enemy, it gives me an error that keeps stacking rapidly, up to a few hundred in a few seconds. This is the error:
NullReferenceException: Object reference not set to an instance of an object
enemyParent.FixedUpdate () (at Assets/AI/enemyParent.cs:8)