- Home /
Turn on/off Script
There is a large box with a small box inside. The small box has a script on it. How would you turn the small box script OFF when it passes through the larger box? and then turn back ON if you go back inside the larger box?
Edit: Leaving cryptic answers only confuse me as a beginner :( Anyone willing to write the full answer with it somewhat explained? I'd love to learn to write my own but I always get broken code that doesn't make sense to me just yet. :)
Answer by rednax20 · Aug 14, 2013 at 05:45 PM
Try looking into OnTrigger Enter/Stay/Exit.
http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnTriggerEnter.html
http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnTriggerExit.html
http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnTriggerStay.html
Quishtay all the way™
I found this line in another question, think it would help? I suck at writing code lol seems like its on the right track though?
GetComponent("SmallBoxScript").enabled = false;
Answer by SubatomicHero · Aug 14, 2013 at 05:46 PM
You don't turn off the script you deactivate the small cube gameobject when the collision between the two cubes occurs and then reactivate the cube once the collision has stopped.
gameObject.SetActive (false); //deactivate
gameObject.SetActive (true); //activate
Answer by $$anonymous$$ · Aug 14, 2013 at 06:52 PM
try this you want to put this on your big box make sure you set the big box as trigger and dont forget to assign you gameobject to smallbox
Var smallbox : GameObject;
function OnTriggerEnter (){
smallbox.SetActive (false);
}
function OnTriggerExit(){
smallbox.SetActive (true);
}
shutoff.js(5,35): BCE0019: 'enabled' is not a member of 'UnityEngine.Component'
hmmmmmmmm? lol
Your answer
Follow this Question
Related Questions
How to turn on a light by pressing and after few seconds turn it off 1 Answer
Light script flashes on and off 1 Answer
My night vision wont turn on/off 0 Answers
UI Button ON/Off 3 Answers