I’ve got an enemy that is supposed to be destroyed depending on whether a boolean value is true or false. The problem is, once I one of them is destroyed the assigned object is removed from the Destroy function and it no longer activates.
public float MaxDistance = 10.0f;
public float MoveSpeed = 5.0f;
public float MinDistance = 0.0f;
public Transform Target;
public float LookAtDistance = 10.0f;
public float Distance;
public float Damping = 5.0f;
private Quaternion rotation;
public bool parenting = false;
public float curHealth;
private PlayerHealth_Test playerHealth;
//private Bank rolling;
public GameObject leech;
public bool rolling = false;
void Start (){
Target = GameObject.FindGameObjectWithTag ("Player").transform;
playerHealth = Target.GetComponent<PlayerHealth_Test>();
parenting = false;
// rolling = Target.GetComponent<Bank>();
leech = GameObject.FindGameObjectWithTag ("Leech");
}
void OnTriggerEnter(Collider Player)
{
transform.parent = GameObject.FindGameObjectWithTag("Player").transform;
parenting = true;
}
void Update () {
leech = GameObject.FindGameObjectWithTag ("Leech");
Distance = Vector3.Distance (Target.position + Target.transform.forward, transform.position);
if (Distance < LookAtDistance)
{
LookAt();
}
if (Distance > MinDistance && Distance < MaxDistance && parenting == false)
{
follow ();
}
if (rolling == true)
{
parenting = false;
GameObject.DestroyObject(leech);
}
}
void FixedUpdate ()
{
if (parenting == true)
{
playerHealth.CurrentHealth -= 0.05f;
}
}
void LookAt(){
rotation = Quaternion.LookRotation (Target.localPosition - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
}
void follow(){
transform.Translate(Vector3.forward * MoveSpeed * Time.deltaTime);
}
}
(The commented out section are the the actual code I’ll use when I bring this script into the main project, just testing things for now, I’m just turning rolling on and off in the inspector currently).
leech is the object that needs to be destroyed. The first time this occurs leech is removed from the DestroyObject() function. I tried to fix this by assigning leech every update but now every copy of the object is destroyed when rolling becomes true on a single one of the object. I’ve also tried DestroyImmediate() having no idea what that does specifically but that didn’t do anything different.