infinite loop in AI

Hey there

I want to make an AI-Robot in my Game.

For this, the robot scan an object.

I wrote following script-part:

while(hilf == true){																			//obere rechte ecke suchen
				Hals.transform.Rotate(0.0f,ObjektScanInterval,0.0f);
				if((Physics.Raycast (ray,out hit, 100)) == true){
					hilf = true;
				}else {
					hilf = false;
				}		
			}

the problem is, that this script is an infinite loop. I can´t see, where is the problem.

Please help me

Thank you

Maybe the spacing between the Physics.Raycast method and its parameters?

Put it in a coroutine and yield each interval. It’s trying to execute the entire thing in one pass.

It does the rotatation. It checks if raycast is true, and if so, it sets hilf to true and if not, it’s false… so based on what you’re describing, it’s always hitting something and the raycast is never false.

In other words, put less scan-able stuff in your game?

:smile: thank you for this idea :smile: it was the right thing :smile: thank you sooooo much :smile:

my euphoria was to fast yet.

my game is running, but the script don’t jump out this while, because, like lockbox said, hilf is never false.

how can i fix it ?? my script is like this:

while(hilf == true){
	Hals.transform.Rotate(0.0f,ObjektScanInterval,0.0f);
	if((Physics.Raycast (ray,out hit, 100)) == true){
	}else {
		hilf = false;
	}	
	yield return null;
}

thanks for helping

What are you trying to accomplish? What is suppose to happen it’s false?

Without knowing that answer to those questions, you can limit it with time, like this:

float startTime = Time.time;
float timeLimit = 5.0f; 

while (hilf == true  (Time.time - startTime < timeLimit) {
 ...
}

or base it on a count, like this:

int count = 0;
maxCount = 20;

while (hilf == true  count < maxCount) {
 ...
 count++;
}

Just two of many ways to get out of that loop…

There’s no code that updates your ray. You’re rotating an object but raycasting the same thing over and over again so of course it’s never going to be false.

I don’t know what going wrong with me -.-
thank you :smile:

ofcourse i have to recalculate the raycast -.-

thanks :smile:

Yeah, is just that the part where you code the if statement: if((Physics.Raycast (ray,out hit, 100)) == true){ is always true and it’s looping because your variable “hilf” is always assigned to be true. As a suggestion I would recommend having boolean variables declared before so that you could validate it in your loop and get out of the loop when they get activated. It saves a lot of time. Good luck! :slight_smile: