This script is attached to my camera, but it keeps locking up.
I am testing to see if one of three cubes have been touched, and then I would like to distinguish which of the three type of cubes it is. Any help would be appreciated.
var hit : RaycastHit;
var tapSound : AudioSource;
while (true)
{
// Use Raycast to pick objects that have mesh colliders attached...
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Input.GetMouseButtonDown (0))
{
if (Physics.Raycast (ray, hit, 100))
{
tapSound.Play();
}
}
}
Ive also encountered problems when using the infinite loop “while(true)” - just as you describe any such loops tend to lock up. My advice (not sitting by my dev-computer at the moment I cant be more specific Im afraid) would be to refrain from them altogether and use the API-functions instead (Update(), etc).
I’m assuming this script is being run in an Update() function of some sort. You’re telling it to do something WHILE it is true. This means, it’ll never pass the current update because it is still in the loop.
Better pseudo code would be
function Update () {
if (true)
// do stuff
}
This way, it’ll continue on to the next Update, still doing what you need it to do.