- Home /
When inside trigger, turn object on, when outside trigger, turn off
says it all really.
i am simply trying to make a java script to turn an object on when the player is inside a trigger cube area and then for the object to turn off on exit of the trigger area. How do i do this?
Answer by LightSource · Apr 06, 2013 at 03:51 PM
Here is an example:
var triggerOn : boolean = false;
function OnTriggerEnter () {
triggerOn = true;
}
function OnTriggerExit () {
triggerOn = false;
}
Awesome, that helps with recognising if the player is in or out of the trigger area. but what would i combine this with to to turn a seperate object on or off? say an object which is attached to my camera.
right, i tried this:
var cube : GameObject;
function OnTriggerEnter () {
cube.GameObject.Active(true);
}
function OnTriggerExit () {
cube.GameObject.Active(false);
}
But alas i get this error everytime i enter and exit the trigger its attached to.
NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cache$$anonymous$$eyName, System.Type[] cache$$anonymous$$eyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cache$$anonymous$$eyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args) UnityScript.Lang.UnityRuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args, System.Type scriptBaseType) onoff.OnTriggerEnter () (at Assets/onoff.js:5)
its driving me insane!
Your using .active wrong. Like this:
GameObject.active = false;
Example:
var cube : GameObject;
var triggerOn : boolean = true;
function Update () {
if (triggerOn == true) {
cube.active = false;
}
}