Unity Hanging Up With This Code

I’m still trying to figure this scripting thing out and I’m not sure what I’m doing wrong here so any help would be greatly appreciated. So, I’m basically trying to spawn a random object from 4 pre-defined objects then figure out when that object is done falling to the ground and spawn another random object when it stops moving. I’m not sure what’s wrong since I had gotten it to work a couple times (work as in run, not as in function the way I’m trying to make it function), but when I changed it a little to try to make it run better, it started giving me the Mac rainbow pinwheel thing again so I took it out and now it’s still doing it after restarting, etc. Here’s what I have so far.

var randomShape : int;
var shapeOne : GameObject;
var shapeTwo : GameObject;
var shapeThree : GameObject;
var shapeFour : GameObject;
var cutoffVelocity : float = 0.01;
var stopped : boolean = true;

function Start()
{
	randomShape = Random.Range(1,4);
	if(randomShape == 1)
	{	
		Instantiate(shapeOne, transform.localPosition, Quaternion.identity);
		shapeTouched = false;
	}
	else if(randomShape == 2)
	{	
		Instantiate(shapeTwo, transform.localPosition, Quaternion.identity);
		shapeTouched = false;
	}
	else if(randomShape == 3)
	{	
		Instantiate(shapeThree, transform.localPosition, Quaternion.identity);
		shapeTouched = false;
	}
	else if(randomShape == 4)
	{	
		Instantiate(shapeFour, transform.localPosition, Quaternion.identity);
	}
	GenerateNumber();
}
function GenerateNumber()
{
	randomShape = Random.Range(1,4);
	SpawnShape();
}
function SpawnShape()
{
	Check();
	if (stopped)
	{
		if(randomShape == 1)
		{	
			Instantiate(shapeOne, transform.localPosition, Quaternion.identity);
			shapeTouched = false;
		}
		else if(randomShape == 2)
		{	
			Instantiate(shapeTwo, transform.localPosition, Quaternion.identity);
			shapeTouched = false;
		}
		else if(randomShape == 3)
		{	
			Instantiate(shapeThree, transform.localPosition, Quaternion.identity);
			shapeTouched = false;
		}
		else if(randomShape == 4)
		{	
			Instantiate(shapeFour, transform.localPosition, Quaternion.identity);
		}
		GenerateNumber();
	}
}

function Check()
{
	stopped = true;
	var shapes = new Array (GameObject.FindGameObjectsWithTag("Shape"));
	for ( var shape in shapes )
	{
		if(shape.rigidbody.velocity.magnitude > cutoffVelocity || shape.rigidbody.angularVelocity.magnitude > cutoffVelocity)
		{
			stopped = false;
			break;
		}
	}
}

Hello there!

You seem to have yourself an infinite loop here.
GenerateNumber calls SpawnShape which calls GenerateNumber.
Be very careful when having this sort of dependancy between functions, you have to make absolutely sure that the loop will be broken at some point, otherwise the program will simply try to execute it forever.

So then how would I be able to keep the objects spawning without the infinite loop? Could I simply add a condition in where it’ll stop spawning if something happens to stop the loop? Thanks for the help!

you could:

  1. continue your spaghetti and remove else if, just leaving if’s
  2. or you could do it properly with switch and cases

Never mind, I got it worked out, thanks!

Or 3) You could do it best by using arrays, and not use any if/switch/anything.

As a side note, cmasterenko, you want to read the docs on Random.Range, since it’s not doing quite what you think it’s doing.

Also, this:

var shapes = new Array (GameObject.FindGameObjectsWithTag("Shape"));

in unnecessary, since FindGameObjectsWithTag already returns a GameObject array, which is a lot faster than using Array. Just do this:

var shapes = GameObject.FindGameObjectsWithTag("Shape");

–Eric