- Home /
OnTriggerEnter called before Start
Hi everyone,
I just started a new job and inherited an incredibly buggy codebase, which I am trying to debug. At one point in the code, a NullReferenceException is being thrown because OnTriggerEnter is being called for a script before its Start function is being called (so none of the variables are defined yet).
My understanding is that Start should be called before OnTriggerEnter. The only exception is that a disabled object will still receive trigger events, but I dropped
Debug.Log(gameObject.activeSelf);
Into the OnTriggerEnter() function and can confirm that the gameObject IS active when the trigger event is happening. Are there any other situations in which OnTriggerEnter might be called before Start?
This is Unity 5.0.1 by the way. Thank you!
EDIT: Oh and I suppose it's worth mentioning that the scene is set up with a rigidbody inside the trigger at start.
I'm having the same problem
var car : Transform;
var player : Transform;
var exitPoint : Transform;
var doorTriggerLeft : Transform;
var PlayerCamera : Camera;
var CarCamera : Camera;
var isPlayerVisible : boolean;
function Start(){
WaitForSeconds(5); isPlayerVisible = false;
}
function Update(){
if (isPlayerVisible&& Input.GetButtonUp("Action")){
// make player invisible and still standing
player.gameObject.SetActive(false);
// player.gameObject.activeself = false;
// parent player to Exit Point
player.parent = exitPoint.transform;
player.transform.localPosition = Vector3(-1.5,0,0);
// parent PlayerParent to car
exitPoint.parent = car.transform;
exitPoint.transform.localPosition = Vector3(-0.5,0,0);
// Enable Car as controllabe object
GameObject.Find("car").GetComponent("CarUserControl").enabled=true;
PlayerCamera.enabled = false;
CarCamera.enabled = true;
}
else
{
if (Input.Get$$anonymous$$eyUp("y")){
// make character visible again
player.gameObject.SetActive(true);
// player.gameObject.activeself = true;
// unparent player from everything
player.transform.parent = null;
// parent Exit Point to door Trigger
exitPoint.parent = doorTriggerLeft.transform;
// disable car as controllable
GameObject.Find("car").GetComponent("CarUserControl").enabled=false;
PlayerCamera.enabled = true;
CarCamera.enabled = false;
}
}
}
function OnTriggerEnter(Player : Collider) {
isPlayerVisible = true;
}
function OnTriggerExit(Player : Collider) {
isPlayerVisible = false;
}
Answer by TGD · Jul 15, 2015 at 07:20 PM
I fixed it, apparently having an object (terrain, rigidbody, etc.) in the trigger makes it activate at runtime.
TGD, I was aware that the trigger event activates at runtime, but I thought that the Start() function should still be called immediately before OnTriggerEnter(). Were you having an issue where OnTriggerEnter() was getting called before Start()?
Answer by blizzardeagle · Sep 28, 2015 at 06:18 AM
I just had a similar problem. I fixed it by putting my object initialization code in Awake(), rather than Start().
From the unity manual:
Start is called on the frame when a script is enabled just before any of the Update methods is called the first time.
Awake is used to initialize any variables or game state before the game starts.
Answer by mmanle01 · Nov 26, 2017 at 07:06 PM
I could be wrong, but I'm pretty sure that triggers are fired even if the game objects are disabled (meaning Start() has never fired off yet).
From the Documentation: https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html
OnTriggerEnter is called when the Collider other enters the trigger.
This message is sent to the trigger Collider and the Rigidbody (if any) that the trigger Collider belongs to, and to the Rigidbody (or the Collider if there is no Rigidbody) that touches the trigger. Notes: Trigger events are only sent if one of the Colliders also has a Rigidbody attached. Trigger events will be sent to disabled MonoBehaviours, to allow enabling Behaviours in response to collisions. OnTriggerEnter occurs on the FixedUpdate after a collision. The Colliders involved are not guaranteed to be at the point of initial contact.
Triggers are not sent if the GameObjects are disabled, however, they are fired if the script that contains the trigger is disabled. The GameObject still needs to be enabled for the trigger to fire.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
What is the difference between these two scripts? 1 Answer
How to Destroy items in Match 3. 2 Answers