- Home /
Cannot stop character movement upon trigger
I want to make it so when the player walks over this trigger it stops movement but still lets them move the camera
function OnTriggerEnter(other : Collider) {
GetComponent("CharacterMotor").enabled = false;
}
Thats what i've got but it comes up with the error "Object reference not set to an instance of an object"
Any help would be much appreciated
Answer by Hybris · Apr 28, 2012 at 05:08 PM
has to be:
GetComponent(CharacterMotor).enabled = false;
without the ""
Indeed.I just punched my screen reading the "has to be:" and my girlfriend behind told me "Don't let him get away with it!!". But yes, without "" is better. So for serious information, both are working but without the "" is more efficient.
Answer by Lo0NuhtiK · Apr 28, 2012 at 05:08 PM
function OnTriggerEnter(other : Collider){
if(other.gameObject.CompareTag("Player")){
var charMotor : CharacterMotor ;
charMotor = other.GetComponent(CharacterMotor) ;
charMotor.enabled = false ;
}
}
...or just using [ other.GetComponent(CharacterMotor).enabled = false ;] would probably work also... what I think you're missing though, is simply the 'other.' part since I'm assuming your script is on another object and you need to get the character motor script off of the object that enters it (what your code does at the moment, is try to get CharacterMotor from the object which this script is attached to, not the object that enters it)