- Home /
Making OnTriggerEnter execute on startup
I have a static collider that can have objects inside it when scene starts. But the trigger does not activate until one of them moves, which is undesirable.
I tried looking for solutions but it seems like a Unity flaw. Atm I'm using this hack:
private Vector3 positionHack;
void Awake() {
positionHack = gameObject.transform.position;
gameObject.transform.position = gameObject.transform.position + Random.insideUnitSphere * 100f;
}
void Start() {
gameObject.rigidbody.MovePosition(positionHack);
}
But it requires a rigidbody (I'd prefer not to have one). Is there a solution to this or at least a better hack? Can you manually trigger a physics check?
I thought triggers had fired for me on start before, but couldn't remember so I just now threw a sphere and a cube into a new scene, scaled the cube up to contain the sphere (with plenty of extra room inside also), put a rigidbody on the cube, then put a script on the cube with nothing but ->
void OnTriggerEnter(Collider other)
{
Debug.Log("Entered") ;
}
...on it... Pushed the play button... the debug printed to the console.
After that I added this ->
void Start()
{
OnTriggerEnter(null) ;
}
..to that same script and pushed play again. Got two debugs printed.
After that, I removed the script and the rigidbody from the cube, and added the script and a rigidbody to the sphere. Pushed play and got two debugs again. So, idk why yours isn't working. Try the trigger call in the start method and see if you get at least one debug that way.
Edit : And no, neither of them were moved. Their useGravity = false
Oh thanks! Neither object had a rigidbody. It's really weird that Unity doesn't support this because once any rigid body starts the trigger, it can be triggered on/off even by objects without rigid bodies.
Your answer
Follow this Question
Related Questions
Colliding two GameObjects 1 Answer
Can't figure out how to use multiple triggers in single scene 1 Answer
Array for multiple cameras? 2 Answers
Register Particles going through Trigger collider 1 Answer
OnTriggerEnter doesnt see enemy 1 Answer