OnTriggerEnter not allowing function from referenced script

I don’t have much experience with triggers, so I’m having an issue where I need to use a function from another script when I trigger the trigger object, I’ve refenced the script to use the function but i can’t figure out how to get the function to activate in the OnTriggerEnter function. I use to have it using colliders with OnCollisionEnter and it worked but i wanted it to be a trigger instead and now it doesn’t work.
any help to get this function to work would be much appreciated

public class SpeedBoost : MonoBehaviour
{
    public GameObject speedBoost; // reference to the trigger object
    public int speedBoostmultiplier = 5; // interger to add to the value maxspeed in other script

    void Update() // not relevant to the problem
    {
        float z = Mathf.PingPong(Time.time, 1f);
        Vector3 axis = new Vector3(1, 1, z);
        transform.Rotate(axis, 1f);


    }

    void OnTriggerEnter()
    {
        speedBoost.SetActive(false); //just sets the trigger object to not active
//this is the issue down below vvv
        PrometeoCarController Carspeed = GetComponent<PrometeoCarController>(); // referencess the other script to access the function
        if (Carspeed != null)
        {
            Carspeed.SpeedIncrease(speedBoostmultiplier); //activate the function in the other script
        }
    }
}

and this is the function from the other script that is referenced:

[code=CSharp]public void SpeedIncrease(int increase) // the fuction i need to use
    {
        maxSpeed += increase; // adds the maxspeed value by the increase int(speedBoostmultiplier)
    }

A few things to cover here. Right now your OnTriggerEnter isn’t going to be called as it’s not set up correctly. Refer to the docs: Unity - Scripting API: Collider.OnTriggerEnter(Collider) .

Right now you’re just missing the correct signature (ergo, passing the Collider through).

As it passes through the Collider that triggered it, you can use that to get a reference to anything relevant:

private void OnTriggerEnter(Collision other)
{
    gameObject.SetActive(false);
 
    if (other.TryGetComponent(out PrometeoCarController controller))
    {
        controller.SpeedIncrease(speedBoostmultiplier);
    }
}

I’ll also note that you don’t need the public GameObject speedBoost; field as every component has a .gameObject property to give access to object it lives on.

1 Like

I thought this too! Turns out that probably for legacy reasons the argument-free version gets hit too:

using UnityEngine;

public class OTE : MonoBehaviour
{
    void OnTriggerEnter()
    {
        Debug.Log( "Booya!");
    }
}

8738244--1183332--Screen Shot 2023-01-17 at 9.32.06 PM.png

Obviously still need a Rigidbody and that stuff.

And if you give it some other random argument (such as an int) it will complain bitterly that the signature does not match.

// doesn't like this:
    void OnTriggerEnter(int x)

8738244--1183329--Screen Shot 2023-01-17 at 9.30.39 PM.png

Well there you go. I guess it makes sense it can still invoke the magic method still if it has no parameters.

Might be useful behaviour to take advantage of at some point.

1 Like