- Home /
Enemy movement after death
Hello guys I'm new to unity and coding in general , and trying to make my first game
So I managed to make the enemy show "death animation" after jumping on its head, but it still moves untill it's destroyed
Tried getting to the scrips of the enemy from the player's, but I get a console error- object reference not set to an instance of an object (line with EnemyMove.speed)
that's the player's script (cut it short for ya, tell me if I need to add the full script)
public class PlayerMovement : MonoBehaviour {
//kill enemies
public GameObject jumpkill;
private Animator DieAnm;
private EnemyMove EnemyMove;
// Start is called before the first frame update
void Start()
{
//enemy
EnemyMove = GetComponent<EnemyMove>();
DieAnm = jumpkill.GetComponent<Animator>();
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Stomp")
{
Destroy(jumpkill, 1);
DieAnm.SetTrigger("Die");
EnemyMove.speed = 0;
}
}
}
that's the enemy script
public class EnemyMove : MonoBehaviour {
public float speed;
public Vector2 movement;
public Animator DieAnm;
// Start is called before the first frame update
void Start()
{
speed = 1 * Time.deltaTime;
movement = new Vector2(speed, 0);
}
// Update is called once per frame
void Update()
{
transform.position = new Vector2(transform.position.x + movement.x, transform.position.y);
if (transform.position.x >= 41)
{
movement = new Vector2(-speed, 0);
transform.localScale = new Vector2(-0.45f, 0.45f);
}
if (transform.position.x <= 34)
{
movement = new Vector2(speed, 0);
transform.localScale = new Vector2(0.45f, 0.45f);
}
}
}
Well first of all, what is jumpkill? and can you share a screen shot or better yet a GIF of whats going on?
$$anonymous$$aybe the OnCollisionEnter2D
isnt being called. You can see if this is the case by placing Debug.Log()
inside the condition. if (collision.gameObject.tag == "Stomp"){Debug.Log("PLAYER STO$$anonymous$$P");}
Then when colliding with the player you check if the Debug.Log()
is being called. If not then your collision isnt being detected. $$anonymous$$aybe you havent set the "Stomp" tag to the object.
Answer by frederikedel · Jul 18, 2020 at 12:55 PM
@yakirh95 I don't quite understand where the enemymove Script is attached to and what jumpkill is, but I can probably explain the Error Message:
Destroy(jumpkill, 1);
DieAnm.SetTrigger("Die");
EnemyMove.speed = 0;
If I understood correctly, EnemyMove is attached to jumpkill?!. If thats the case, you're destroying the GameObject and then trying to access the script, which was attached to jumpkill. But its gone and the reference you did with EnemyMove = GetComponent<EnemyMove>();
doesnt exist anymore. Thats why it say Reference not set to an Instance.. . Just try swapping the speed = 0 and the Destroy() Line.
I'm sorry if I wasnt clear. Enemy$$anonymous$$ove is the enemy's script. I created a collider on the enemy's head (tagged it "stomp"), and then created GameObject in the player's script (called it jumpkill), and dragged the enemy into it. whenever I hit the collider, the enemy is destoryed.
changing the lines order did'nt work =(
But in this case, this line Enemy$$anonymous$$ove = GetComponent<Enemy$$anonymous$$ove>();
makes no sense. You're searching for the Enemy$$anonymous$$ove Script in the PlayerController, which is probably on the player. But the Script is on the Enemy.So it returns null exception, because it cannot be found. Here's what you could try: Enemy$$anonymous$$ove = jumkill.GetComponent<Enemy$$anonymous$$ove>();
If you're unsure what you have to reference you can write:
[SerializeField] private Enemy$$anonymous$$ove Enemy$$anonymous$$ove;
SerializeField allows you to reference something through the inspector while still having the gamobject be private.
Answer by gblim · Jul 18, 2020 at 03:23 PM
Try changing EnemyMove = GetComponent<EnemyMove>();
to EnemyMove = GameObject.FindObjectOfType<EnemyMove>();
Answer by Bezoro · Jul 19, 2020 at 08:13 PM
You are trying to access the enemy speed after marking it for destruction, Unity won't allow that so it throws an exception.
Btw, handling the logic for killing enemies directly in the player is a pretty bad idea. The enemy itself should handle all that.
So instead of the player getting references to the enemy and handling all the logic for killing the enemy it should simply call a function inside the enemy class that handles everything.
The way you have it setup right now is very very bad practice and will bite you later.
There is no reason for the player to know how or why the enemy works the way it works, it should just check for the collision then tell the enemy to die.