In Unity, how can my code detect when a trigger occurs with a specific distinct object?
I already tried several things, such as:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public GameObject DualCannon_PU;
void OnTriggerEnter2D (Collider2D coll) {
if (coll.name == "DualCannon_PU") {
Debug.Log ("DualCannon PowerUp");
}
}
}
But it doesn't work: it doesn't trigger anything and "DualCannon PowerUp" does not appear in console.
In player gameObject "is trigger" is checked and my powerUp (DualCannon_PU) "is trigger" is not checked.
I noticed that I had 2 "Player Controller (Scripts)", I deleted the first one but the problem still persists...
an example of what I want:
I have 4 gameObjects and both have 2D Colliders
- A. Player ship ("is trigger" is on)
- B. Enemy projectiles
- C. Health PowerUp
- D. Dual Cannon PowerUp
Condition I want:
- A is triggered by B, C, and D
- When B triggers with A in A script (Player.cs) executes:
health -= laser.GetDamage();
- When C triggers with A in A script (Player.cs) executes:
health = health + 10;
- When D triggers with A in A script (Player.cs) executes
isDualCannon = true