I need help with a Block Spawner

I am trying to make a block which can spawn different blocks…I am not very good at coding unfortunately…can anyone help? this is what i managed till now (its in java script):

#pragma strict

function Start () {

}

static function Instantiate( original : Object, position : Vector3, rotation : Quaternion ) : Object {
    transform.position = Random.insideUnitSphere * 10;
}

function Update () {

}

You need to break it down …

  • 1

Let’s first look at Instantiate : Unity - Scripting API: Object.Instantiate

From the Unity Script Reference above we get an example script, and can make a simple script :

var prefabBlock : Transform;

function Start()
{
	// Instantiates 10 copies of prefabBlock each 2 units apart from each other
	for ( var i : int = 0; i < 10; i ++ ) 
	{
		Instantiate( prefabBlock, Vector3(i * 2.0, 0, 0), Quaternion.identity );
	}
}
  • 2

Great, so at start 10 blocks are spawned, 2 units apart from each other though! Let’s make them spawn at a specific place. You want to use a ‘Spawn Block’, so add that to the script, then use its position in the Instantiate. Make sure you have a ‘SpawnBlock’ on the stage, then drag it into the script’s inspector :

var spawnBlock : Transform;

var prefabBlock : Transform;

function Start()
{
	// Instantiates 10 copies of prefabBlock each 2 units apart from each other
	for ( var i : int = 0; i < 10; i ++ ) 
	{
		Instantiate( prefabBlock, spawnBlock.position, Quaternion.identity );
	}
}

Now all 10 cubes should spawn at the same position as the spawnBlock object.

  • 3

Next to look at making it more complex, let’s change it so that it spawns a block every time the SpaceBar is pressed. We can put the instantiate in a function, and then just call that function when we want a block. Then we need to check every update for if the SpaceBar has been pressed :

var spawnBlock : Transform;
var prefabBlock : Transform;

var spawnTime : float = 3.0;

function Update()
{
	if ( Input.GetKeyDown(Keycode.Space) )
	{
		SpawnTheBlock();
	}
}

function SpawnTheBlock()
{
	Instantiate( prefabBlock, spawnBlock.position, Quaternion.identity );
}
  • 4

Now we can spawn enemies, but how to make it so the enemys are spawned by themselves, and at random intervals? For this, there is a handy command called InvokeRepeating : Unity - Scripting API: MonoBehaviour.InvokeRepeating

var spawnBlock : Transform;
var prefabBlock : Transform;

var spawnTime : float = 3.0;

function Start()
{
	InvokeRepeating( "SpawnTheBlock", 0.1, spawnTime );
}

function SpawnTheBlock()
{
	Instantiate( prefabBlock, spawnBlock.position, Quaternion.identity );
}
  • 5

Hey, it’s nearly working, but how many Blocks have been spawned? Let’s add a counter :

var spawnBlock : Transform;
var prefabBlock : Transform;
var spawnTime : float = 3.0;

var spawnCount : int = 3.0;

function Start()
{
	InvokeRepeating( "SpawnTheBlock", 0.1, spawnTime );
}

function SpawnTheBlock()
{
	Instantiate( prefabBlock, spawnBlock.position, Quaternion.identity );
	spawnCount += 1; // Note : this can be written as spawnCount ++;
}

function OnGUI()
{
	GUI.Box( Rect( (Screen.width * 0.5) - 50, 10, 100, 25 ), "Blocks : " + spawnCount.ToString() );
}
  • 6

Nice, … hey wait! there are too many Blocks! Lets cap the amount of blocks that can be spawned. We need to stop the InvokeRepeating : Unity - Scripting API: MonoBehaviour.CancelInvoke

var spawnBlock : Transform;
var prefabBlock : Transform;
var spawnTime : float = 3.0;
var spawnCount : int = 3.0;

var maximumSpawnCount : int = 10;

function Start()
{
	InvokeRepeating( "SpawnTheBlock", 0.1, spawnTime );
}

function SpawnTheBlock()
{
	Instantiate( prefabBlock, spawnBlock.position, Quaternion.identity );
	spawnCount += 1; // Note : this can be written as spawnCount ++;
	
	if ( spawnCount >= maximumSpawnCount )
	{
		CancelInvoke( "SpawnTheBlock" );
	}
}

function OnGUI()
{
	GUI.Box( Rect( (Screen.width * 0.5) - 50, 10, 100, 25 ), "Blocks : " + spawnCount.ToString() );
}
  • 7

Phew, now only 10 blocks spawn. Wow, this is all pretty good. But I forgot something, you want to spawn a different random block each time. For this we need places to put the blocks, and a Random number to choose a block, then if that : Unity - Scripting API: Random.Range

var spawnBlock : Transform;

var prefabBlock1 : Transform;
var prefabBlock2 : Transform;
var prefabBlock3 : Transform;

var spawnTime : float = 3.0;
var spawnCount : int = 3.0;
var maximumSpawnCount : int = 10;

function Start()
{
	InvokeRepeating( "SpawnTheBlock", 0.1, spawnTime );
}

function SpawnTheBlock()
{
	var rnd : int = Random.Range( 1, 4 ); // 1 to 3 is written 1,4 as the 4 is never returned, see the docs
	
	if ( rnd == 1 )
	{
		Instantiate( prefabBlock1, spawnBlock.position, Quaternion.identity );
	}
	else if ( rnd == 2 )
	{
		Instantiate( prefabBlock2, spawnBlock.position, Quaternion.identity );
	}
	else if ( rnd == 3 )
	{
		Instantiate( prefabBlock3, spawnBlock.position, Quaternion.identity );
	}
	
	spawnCount ++;
	
	if ( spawnCount >= maximumSpawnCount )
	{
		CancelInvoke( "SpawnTheBlock" );
	}
}

function OnGUI()
{
	GUI.Box( Rect( (Screen.width * 0.5) - 50, 10, 100, 25 ), "Blocks : " + spawnCount.ToString() );
}

And there we have it, one multi-random-block spawning script, to spawn at BigBlocks position =]

To spawn different blocks (prefabs) you are going to need a way to provide your spawn method a list or some form of set to choose from.

Example in C#:

//Somewhere in your setup you would need to define the prefabs for each block type...in this case all would be subclassed from CustomBlockThing.
Public List<CustomBlockThing> BlockTypesList = new List<CustomBlockThing>();

//...assume we have populated the list. Since the list is set to Public, you could do so by dragging objects into it in the editor.

//Here is our method to spawn new blocks. It selects 1 randomly from the types of blocks available in the previously declared BlocksList
GameObject SpawnBlocks(int blockType = -1){

	//This allows us to specify the type if we do not want random...
	if (blockType == -1){

		CustomBlockThing randomBlock;

		//Generate a random number in the range of 1 to the number of block types we have defined.
		int randomBlockType = Random.Range(1, BlockTypes.length);
		
		
		//Here we use that random number to select a block type to spawn
		randomBlock = Instantiate(BlockTypes[randomBlockType], transform.position, transform.rotation) as CustomBlockThing;

		//Pass a reference to the newly created random typed CustomBlockThing to the caller
		return randomBlock;
	}
	else{
		
		//should probably generate an error of some type if the blockType is out of range, but I will instead assume we ALWAYS return a block of some type...
		if (blockType > BlockTypes.length || blockType < 1){
			CustomBlockThing randomBlock;

			//Generate a random number in the range of 1 to the number of block types we have defined.
			int randomBlockType = Random.Range(1, BlockTypes.length);

			//Here we use that random number to select a block type to spawn
			randomBlock = Instantiate(BlockTypes[randomBlockType], transform.position, transform.rotation) as CustomBlockThing;
			
			//Pass a reference to the newly created random typed CustomBlockThing to the caller
			return randomBlock;
		}
		
		CustomBlockThing specificBlock;
		
		//note that we still use the list, but ask for a specific item/index based on the blockType variable.
		randomBlock = Instantiate(BlockTypes[randomBlockType] transform.position, transform.rotation) as CustomBlockThing;

		//Pass a reference to the newly created, specific type of, CustomBlockThing to the caller
		return specificBlock;
	}
}

If you are simply randomizing the values for each block as it is spawned, you may not need to do the above, but instead something like (psueudo-code)

newBlock = instantiate a new block object at position and heading

newBlock.height = some random value in a limited range
newBlock.position = some random offset
newBlock.velocity = ...
newBlock.otherProperty = some other random value

done.

thanks!, actually i managed abit further…the biggest problem I have now is rendering all the objects I have…it takes forever…and im planning to add many more…

#pragma strict

function Start () {

}


// Instantiates 100 copies of prefab each 2 units apart from each other

var prefab : Transform;
var prefab1 : Transform;
var prefab2 : Transform;

for (var i : int = 0;i < 100000000; i++) {
    Instantiate (prefab, (Random.insideUnitSphere * 5), Quaternion.identity);
}

for (var j : int = 0;j < 100000000; j++) {
    Instantiate (prefab1, (Random.insideUnitSphere * 5), Quaternion.identity);
}

for (var k : int = 0;k < 100000000; k++) {
    Instantiate (prefab2, (Random.insideUnitSphere * 5), Quaternion.identity);
}




function Update () {

}