- Home /
Enemy attacking enemy when defeated
hi, I'm trying to make a 2d top down game where if I defeat an enemy, then the enemy fights for me. I'm trying to destroy the original enemy object when health = 0 and will set is Dead bool to true, then the other enemy will spawn and attack enemies. but when i kill the enemy, the second enemy doesn't show. here is my code for the second enemy script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class goblinzob : MonoBehaviour { public float speed; private Transform target; private Transform target2; public Animator animator; public Animator aNimator; public Transform homePos; public int maxHealth = 50; public int currentHealth; public HealthBar healthbar; Vector2 movement; public EnemyFollow ef;
// Start is called before the first frame update
void Start()
{
target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
target2 = GameObject.FindGameObjectWithTag("Enemy").GetComponent<Transform>();
currentHealth = maxHealth;
healthbar.SetMaxHealth(maxHealth);
gameObject.SetActive(false);
}
// Update is called once per frame
void Update()
{
movement.x = target.position.x - transform.position.x;
animator.SetFloat("Horizontal", movement.x);
animator.SetFloat("Speed", movement.sqrMagnitude);
aNimator.SetFloat("Horizontal", movement.x);
aNimator.SetFloat("Speed", movement.sqrMagnitude);
if(Vector2.Distance(transform.position, target2.position) < 300 && ef.isDead == true)
{
transform.position = transform.position = Vector2.MoveTowards(transform.position, target2.position, speed * Time.deltaTime);
}
else if (ef.isDead == true)
{
transform.position = transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
}
if(Vector2.Distance(transform.position, target2.position) < 50f && ef.isDead == true)
{
aNimator.SetBool("IsAttacking", true);
}
else if (ef.isDead == true)
{
aNimator.SetBool("IsAttacking", false);
}
if(Vector2.Distance(transform.position, target.position) < 40f && ef.isDead == true)
{
animator.SetBool("HomePosition", true);
}
else if (ef.isDead == true)
{
animator.SetBool("HomePosition", false);
}
if(Vector2.Distance(transform.position, target2.position) < 40f && ef.isDead == true)
{
animator.SetBool("HomePosition", true);
}
else if (ef.isDead == true)
{
animator.SetBool("HomePosition", false);
}
if(ef.isDead == true)
{
gameObject.SetActive(true);
transform.position = target2.position;
}
}
public void TakeDamage(int damage)
{
currentHealth -= damage;
healthbar.SetHealth(currentHealth);
}
}
Answer by logicandchaos · Apr 03, 2021 at 12:57 PM
gameObject.SetActive(true); will not work, a gameObject can not activate itself. When a gameObject is disabled none of the scripts attached to it run. What you could do is when you kill the enemy the player gets a reference to it and activates it, or you can keep all the references in a manager script.
Your answer
Follow this Question
Related Questions
Enemy stops patrolling when player comes close 2 Answers
Health to come above enemy when clicked 1 Answer
How to make arch knockback 3 Answers
How to implement proper enemy follow and shoot 0 Answers
How to hide AdMob ads? 1 Answer