# Player is able to interact with objects but enemy isn't able to

Newbie here, I have a player script that interacts with objects just fine and I made an enemy script for the same object and it gets completely ignored:

Below there are 4 scripts. The Player.cs is player movement, Enemy.cs is enemy movement, Speed.cs gives the player a speed boost when they touch the green object named Speed and enemySpeed.cs is TRYING to give the enemy a speed boost when it touches the green object named enemySpeed. Those last two are both attached to the green game object.

Player.cs :

using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
public float moveSpeed = 10f;
void Update()
{
Vector3 position = transform.position;
if (Input.GetKey("w")){
position.y += moveSpeed * Time.deltaTime;
}
if (Input.GetKey("s")){
position.y -= moveSpeed * Time.deltaTime;
}
if (Input.GetKey("d")){
position.x += moveSpeed * Time.deltaTime;

}
if (Input.GetKey("a")){
position.x -= moveSpeed * Time.deltaTime;

}
transform.position=position;
}
}


Enemy.cs :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : MonoBehaviour
{
public Transform player;
private Rigidbody2D rb;
private Vector2 movement;
public float moveSpeed = 5f;
// Start is called before the first frame update
void Start()
{
rb = this.GetComponent<Rigidbody2D>();
}

// Update is called once per frame
void Update()
{
Vector3 direction = player.position - transform.position;
rb.rotation = angle;
direction.Normalize();
movement=direction;
}
private void FixedUpdate(){
moveCharacter(movement);
}
void moveCharacter(Vector2 direction){
rb.MovePosition((Vector2)transform.position + (direction*moveSpeed*Time.deltaTime));
}
}


Speed.cs :

using UnityEngine;

public class Speed : MonoBehaviour
{
public float speedIncrease = 5f;
private void OnTriggerEnter2D(Collider2D collision){
if (collision.CompareTag("Player")){
GameObject player = collision.gameObject;
Player playerScript = player.GetComponent<Player>();
if (playerScript){
playerScript.moveSpeed += speedIncrease;
Destroy(gameObject);
}
}
}
}


enemySpeed.cs :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class enemySpeed : MonoBehaviour
{
public float speedIncrease=5f;
private void OnCollisionEnter2D(Collider2D collision){
if (collision.CompareTag("Enemy")){
GameObject enemy=collision.gameObject;
Enemy enemyScript=collision.gameObject.GetComponent<Enemy>();
if(enemyScript){
enemyScript.moveSpeed+=speedIncrease;
Destroy(gameObject);

}

}
}
}


I'm also getting this error that happened when I finished making enemySpeed.cs:

I have is trigger checked on for the enemy rigidbody2d because when it touches the player with is trigger off, then it will kick the player and the player will start gliding across the map like its on ice or in space.

I come from the Blender Stack Exchange where we provide all resources we feel are relevant to the question so let me know if I left anything out or included too much.

• Did you mean to use OnTriggerEnter2D, not OnCollisionEnter2D? Jun 21 at 16:41
• While you can mix naming styles, it's advisable to pick a convention & apply it consistently. Specifically, since the default for classes in C# is StartingCappedCamelCase, your enemySpeed class should probably be refactored to EnemySpeed instead. This won't solve your current problem, but it makes it easier to avoid annoying syntax errors. Similarly, using a consistent indentation & bracing style makes it easier to read & subsequently to debug. Jun 21 at 17:02
• @DMGregory yes i did. vs code corrected it to that by accident Jun 21 at 20:58
• If it works now, post that as an Answer below. If it doesn't, edit your question to show your corrected code. Jun 22 at 12:12
• @DMGregory sorry i havent been able to get around to testing it yet. Jun 22 at 15:24

Turns out I made a beginner mistake. I mistakenly used OnCollisionEnter2D instead of OnTriggerEnter2D.