OnTriggerEnter2D won't work properly... C#

So I’m working right now on my game for my School and I encountered a weird problem.
Explaining what I want:
The player is consistantly going to the right on the X axis (without pressing anything), enemy are spawning in front og him and coming towards the player (they are moving slowly towards -x), the player has to shoot them before they get behind him, or the player would lose a life. In front of the player is a Trigger (gameobject) which destroys the bullets shot, which didn’t hit anything (so there are not too many objects in the scene at any point). Behind the player is another Trigger (gameobject) which should trigger the enemy to destroy the enemy and substract a life, but I can’t get that part working…
Infos:

  • The Main Camera is follwing the player Position via script
  • the “BulletDestroyer” GameObject (which destroys the bullets infront of the player) is a child of the player, so it will be always infront of the player
  • The player can switch “lanes” by pressing W and S which is chainging the sorting layer on the player (works perfectly fine)

What I tried so far:

  • Make the “BankObj” GameObject (which is the Trigger) a child of the player like the “BulletDestroyer” → My Game glitches out then, the player won’t move anymore and sometimes is moving rapidly back or forward…
  • Make the “BankObj” a child of the camera
  • Giving the “BankObj” a script where the GameObject is following the player position
  • Changing that script, so the “BankObj” is pushing itself forward like the player
    Script on the Enemy
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class VillianScript_WildWest : MonoBehaviour {

    public int health = 10;
    public GameObject[] healthHearts;
    public int dmg = 1;
    public int speed = 2;

    private void Start()
    {
        healthHearts[1].transform.position = new Vector3(transform.position.x, transform.position.y + 15, 0);
    }

    private void OnTriggerEnter2D(Collider2D col)
    {
        print("Kein Treffer!");
        if (col.CompareTag("Bank"))
        {
            print("Treffer!");
            col.SendMessage("TakeDamage", dmg, SendMessageOptions.DontRequireReceiver);
            Destroy(gameObject);
        }
    }

    private void FixedUpdate()
    {
        transform.Translate(-speed * Time.deltaTime, 0, 0f, Space.World);
    }

    public void TakeDamage(int damage)
    {
        health -= damage;
        if (health == 10)
        {
            healthHearts[1].SetActive(true);
        }
        if (health < 10)
        {
            healthHearts[1].SetActive(false);
            healthHearts[0].transform.position = new Vector3(transform.position.x, transform.position.y + 15, 0);
            healthHearts[0].SetActive(true);
        }
        if (health <= 0)
        {
            healthHearts[0].SetActive(false);
            GameObject enemyCtr = GameObject.Find("EnemyController");
            EnemyController enem = enemyCtr.GetComponent<EnemyController>();
            enem.enemysLeft++;
            Destroy(gameObject);
        }
    }
}

Script on the BankObj

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

public class BankPosScript : MonoBehaviour {

    public Transform player;
    Transform yourPos;
    int speed = 4;
   
    // Update is called once per frame
    void FixedUpdate () {
        //gameObject.transform.position = new Vector2(player.position.x - 40f, 0);
        transform.Translate(speed * Time.deltaTime, 0, 0f, Space.World);
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.CompareTag("Villian"))
        {
            Destroy(collision.gameObject);
        }
    }
}

The “BankObj” has the Tag “Bank” and the Enemy has the Tag “Villian”
If the enemy collides with the player on its way to the left, the OnTriggerEnter2D is triggered and is writing into my console “Kein Treffer!” but it won’t trigger with the “BankObj”

I’m frustrating on that problem…

Does it trigger the OnTriggerEnter2D of the BankPosScript at all?

If it doesn’t, there’s most-likely something wrong with the setup rather than the code, unless you made a typo in that tag (I’d also recommend to save the literals in variables declared as const, so that anything that requires those can refer to a single variable).
Ensure that it’s exactly the same. Also make sure the scripts are attached to the object that’ got the collider component attached to it, as it’ll not traverse the hierarchy.

Another thing that comes to mind is that there’s a rigidbody involved when the enemies hit the player, but there’s none that could trigger anything when the other objects interfere with each other. For instance, if only the player has got the rigidbody component, it could have exactly that outcome.

These are just wild guesses though, it appears you’ve already tried alot and it might actually be a tiny detail.

Funny name though. :stuck_out_tongue:

1 Like

Well… As I was reading this, I immediately realised, that the BankObj don’t have a Rigidbody 2D… I attached it on it and it’s working… I’m so dumb. Thank you for mentioning that… :smile:
I’m forgetting those little things since I started my studies in Game Design…

Thank you :stuck_out_tongue:

Greetings