1

I have an enemy with two colliders (a box and a sphere), both with IsTrigger activated. The actual enemy's GameObject is an Empty and under that is my mesh and another Empty Object to instantiate shots (so Empty "ufo" > ufo mesh, Empty "SpawnShot").

Now, I want the enemy to start shooting once he enters an area that has a box collider (also a trigger with the IsTrigger option activated) and has the tag Boundary.

The code attached to my enemy looks like this:

//Public variables
public float speed, frequency, magnitude;

public GameObject shot;
public Transform shotSpawn;
public float fireRate;

//Private variables
private float nextFire;
private bool startFire = false;

private Vector3 pos;

void Start ()
{
    pos = transform.position;
}

void Update ()
{
    //Sine movement
    pos += transform.forward * Time.deltaTime * -speed;
    transform.position = pos + transform.up * Mathf.Sin (Time.time * frequency) * magnitude;

    //Fire
    if (startFire && Time.time > nextFire) {
        nextFire = Time.time + fireRate;
        Instantiate (shot, shotSpawn.position, shotSpawn.rotation);
    }
}

void OnTriggerEnter (Collider other) 
{
    Debug.Log ("hello im the ufo trigger");

    if (other.gameObject.CompareTag ("Boundary")) {
        startFire = true;
        Debug.Log ("Boundary and boolean is " + startFire);         
    }
}

I tested it without boolean to see if it was a problem with the shooting itself, but that works. I've tested the lines with Debugs and the only thing that does not debug is inside the if statement inside the OnTriggerEnter function. So it does trigger, but it does not go in the if statement.

I checked 100 times if everything is tagged and if the trigger option is activated and it is. I don't know what is wrong!

PS: The ufo moves and all, the only thing that is not working is the fire.

EDIT: so I just added another if statement and this one does work (??) and I changed the function like this:

void OnTriggerEnter (Collider other) 
{
    Debug.Log ("hello im the" + other.gameObject.name + "-" + 
other.gameObject.tag + "trigger");

    if (other.gameObject.name == "Boundary") {
        startFire = true;
        Debug.Log ("Boundary and boolean is " + startFire);
    }

    if (other.gameObject.CompareTag ("player_shot")) {
        Destroy(this.gameObject);       
    }
}

It's still not working, even with comparing it with the name instead that the tag. And when my player fires, the enemy is destroyed, so the colliders are working but not the first if statement

derHugo
  • 83,094
  • 9
  • 75
  • 115
bonishadelnorte
  • 75
  • 1
  • 1
  • 7
  • 1
    So the trigger is firing, and your `OnTriggerEnter` method is being called? Are all the parts of the `other` collider correct? Ie. The `gameObject` and tag? Double check the tag spelling as well. – Steve Oct 23 '18 at 01:54
  • Check if the tag "Boundary" has an empty space at the beginning or end. – Tricko Oct 23 '18 at 02:02
  • yes, when the enemy enters the "Boundary", OnTriggerEnter is called, but the if statement is not. "Boundary" has no spaces and I checked again and everything was spelled correctly and everything is tagged – bonishadelnorte Oct 23 '18 at 02:05
  • 1
    Change the debug log at the start of OnTriggerEnter to `Debug.Log("Hello im " + other.gameObject.name + " - " + other.gameObject.tag)` and see what comes out when you are contacting. Checking the tag might give you a clue. – CaTs Oct 23 '18 at 02:09
  • hmm that's interesting, nothing comes up... but why?? Is it because the object has two colliders? It shouldnt matter – bonishadelnorte Oct 23 '18 at 02:29
  • Have you checked the collision matrix? – Ryolu Oct 23 '18 at 02:36
  • 1
    Based on your edit it seems like the error is the enemy is not detecting collision with the Boundary right? This could be due to a huge number of reasons and depends on how your objects have been set up. Just out of curiousity, does it work if you add a kinematic rigidbody to your Boundary? – CaTs Oct 23 '18 at 02:40
  • THAT WORKED! I added a Rigidbody to the Boundary and activated Is Kinematic and it works! I dont understand why though but thank you!! – bonishadelnorte Oct 23 '18 at 03:16
  • See the trigger messages table [here](https://docs.unity3d.com/Manual/CollidersOverview.html) on the end of the page. This will explain to you why it wasn't working for you. – Programmer Oct 23 '18 at 03:25
  • If a gameObject (or a whole hierarchy) is moving then 99% of the time it is good to have Rigidbody(2D), even if it's kinematic and/or you move it using transform. – Nikaas Oct 23 '18 at 06:31
  • As commented on your [other question](https://stackoverflow.com/q/52826650/15498), we don't edit titles, we accept answers to mark questions as answered correctly. There's no answer here for you to accept currently. If this question is a duplicate of the other one, please close it. If not, and you now have an answer, please *post an answer* and then (after the enforced delay) accept it. – Damien_The_Unbeliever Oct 29 '18 at 09:21

0 Answers0