Breaking out of a loop

hi peeps,

Im very confused as to why this do while loop doesnt work. I always get stuck in an infinite loop. It works if i only have one condition but as soon as i insert multiple conditions it doesnt leave the loop.

 do {		
     ran = Random.Range(0,names.length);

     var ran2:int = Random.Range(0,objectComponants[names[ran]].length); // the length counts the componants array within the hashtable
     
	 var chosenComponent:Component = objectComponants[names[ran]][ran2];
 }
 while ((typeof(chosenComponent)!= Transform) || (typeof(chosenComponent)!= ParticleSystem));

Its waiting to find if the chosen component is either a transform or particle system then it should break.

Thanks in advance

I worked out that instead of || you need to use &&. But i have no idea why? I would have thought || is more appropriate as it should break when either of the two types are found. Im afraid whilst && fixes the problem, i dont understand why.

If you want it to break if either one is true, than you do need to use ||.

If you’re using &&, that means both have to be true to continue.

You’re also using not logic, I don’t know if you understand that or not.

When you include !=, that means not equal to. So your

while is saying while the chosenComponent is NOT a Transform, OR the chosenComponent is NOT a Particle System.. do this..

To change the paragraph with your AND (&&) statement, it’s saying this:

While the chosenComponent is NOT a Transform AND while the chosenComponent is NOT a Particle, do this..