- Home /
Help with collide triggers and calls, please.
Hey guys, ive been away from unity scripting for a month or two, and its made me forget lots of the basics. I cant seem to work out why this script isn't working.
var Player : Transform;
function OnTriggerEnter( Player: Collider)
{
var other : run_away_enemy = gameObject.GetComponent(run_away_enemy);
other.change_come ();
}
function OnTriggerExit( Player: Collider)
{
var other : run_away_enemy = gameObject.GetComponent(run_away_enemy);
other.change_run ();
}
When the game is running, if the player enters and exits the collider, these are the errors I get.
"NullReferenceException: Object reference not set to an instance of an object on_trigger_swarm.OnTriggerExit (UnityEngine.Collider Player) (at Assets/Scripts/on_trigger_swarm.js:16)"
and
" NullReferenceException: Object reference not set to an instance of an object on_trigger_swarm.OnTriggerEnter (UnityEngine.Collider Player) (at Assets/Scripts/on_trigger_swarm.js:8)"
I don't think its relevant...but these are the functions that I am trying to call.
function change_come ()
{
Debug.Log ("true");
attack = true;
}
function change_run()
{
Debug.Log ("false");
attack = false;
}
Any help would really be appreciated!
Thanks
Does nobody know? - or did I just post my question too early in the day perhaps...
Answer by sneftel · Jul 24, 2011 at 02:07 PM
Are you sure the object with on_trigger_swarm
attached also has the run_away_enemy
component attached?
I think so, the hierarchy is like this..
Rats_swarm
- Box Collider with trigger
- On_trigger_swarm
-
Rats
- Run_away_enemy
- Box Collider
- $$anonymous$$esh etc
Does that sound the way it should be?
I'm not sure quite what your notation is in that diagram, but it looks like the answer is "no". If the two components aren't on the same object, they won't be able to find each other with GetComponent
. You can use GetComponentInChildren
or GetComponentsInChildren
for this, depending on what you want.
Ah, Thanks Ben. I thought that if the other object was a sub-object in the hierarchy, that you could use GetComponent. Works perfectly now, thanks so much for your help! =D
Your answer
![](https://koobas.hobune.stream/wayback/20220612154127im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Check Trigger Collision's Name/Tag 1 Answer
Trigger not detecting tag 1 Answer
Question on Tag Calling. 1 Answer
How come the Trigger teleports when its not supposed to! 1 Answer