Can't get object to be reactivated by parent.
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)
Sora$$anonymous$$ahiro Hey, thanks for your reply! The enemy doesn't get Destroy()'ed, I just noticed that was some really poor wording. It just becomes deactivated under certain conditions.
Answer by SoraMahiro · Dec 12, 2016 at 05:54 PM
When the Object become SetActive = false;
the object also becomes null, so that answers the null reference exception. However, as for the Parent not being able to set the child to active, i would try using the enabled
method instead. It takes a boolean argument as enabled = true
or enabled = false
. This can be changed by another script while still setting the game object to be inactive or active. Now if you want the object to disappear when it becomes deactivated, you have to options, SetActive
which we seem to have a problem with, or you could actually use the Destroy()
method as an alternative and instantiate a new one after time has elapsed. Either of these methods, I suggest having a Game Manager that controls game functionality such as Object Activation/Deactivation because this will allow you to have a little more functionality, if you'd like any examples of any of the three methods, just let me know.
Hey there, I indeed want the object to disappear when it becomes deactivated. How would I apply destroy, though? Would I have to make the parent destroy the object? Or would I have to make the parent check if it has a child, and otherwise if it doesnt, instantiate an enemy prefab as new child?
You can create the Game $$anonymous$$anager like I said, then run through the motions, and ins$$anonymous$$d of setting the child to deactive, destroy that whole instance of the enemy, parent and all, then use the Game $$anonymous$$anager to instantiate a new prefab of the enemy. There will need to be references between all three objects, and also, to keep the null reference exception from showing up, you'll need to do some work to make it so that the function returns when it can't find the object such as:
if ($$anonymous$$yGameobject != null) {
//Do something
}
else {
return;
}
Ahh, well, I have no idea how to even begin to make a Game $$anonymous$$anager, so I'll have to research that tomorrow! :)
Oh, that's easy, you just need an empty game object and a script attached to it that's called Game$$anonymous$$anager. It also helps if you name the empty Game$$anonymous$$anager as well. But you should only use this for the functionalities of your game that the other objects can't do themselves. Such as instantiating a brand new prefab.
Ahh, alright! I'll be looking into building a respawn function into it then :) I first gave the enemy object a parent with a sort of managing script on it, but a global gamemanager is probably much more transparent to work with. Thanks for your great help so far! :D