- Home /
Question by
alaarhouma · Jan 31, 2014 at 05:03 PM ·
how i can optimize the following code:
function CheckDistance () {
while (true) {
if ((cube1.position - cube2.position).magnitude < 10)
myLight.active = true;
else
myLight.active = false;
yield WaitForSeconds(5);
}
}
Comment
Best Answer
Answer by iwaldrop · Jan 31, 2014 at 06:16 PM
function CheckDistance ()
{
var wait : YieldInstruction = new WaitForSeconds(5);
while (true)
{
myLight.SetActive((cube1.position - cube2.position).sqrMagnitude < distance * distance);
yield wait;
}
}
Answer by fafase · Jan 31, 2014 at 05:06 PM
Active has been replaced by SetActive(bool). InsteAd of using magnitude use sqrMagnitude and 100 instead of 10. You could use a basic timer with float and delta time instead of wait foreclosed, but those won't change much anyway. there is little to improve here.
Your answer
![](https://koobas.hobune.stream/wayback/20220613132209im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
URGENT HELP NEEDED ASAP, TERRAIN PROBLEMS 1 Answer
colliders with touch 0 Answers
Unity3d Controls Not Working Win8 0 Answers
nullreferenceexception object reference not set to an instance of an object 2 Answers