Can't move instantiated prefab

Hi everybody

I’m a beginner with Unity and I’m struggeling with a problem. I’m working on a 3D Tetris and I’m working on the code, that moves the blocks 1 unit per second.

Here is my code so far (the movementscript is on the bottom):

#pragma strict

var startingPoint : GameObject;
var block1 : GameObject;
var block2 : GameObject;
var block3 : GameObject;
var block4 : GameObject;
var moving : GameObject;
private var randomObject : int;
private var block : GameObject;
static var isSet = false;
private var zaehler = 1;

function Update () {
	if(!isSet) //Check if ther is already an active Block
		{
		//Set random block
		randomObject = Mathf.Round(Random.Range(1.0,4.0));
		switch (randomObject) 
			{
			case 1.0:
				block = block1;
				break;
			case 2:
				block = block2;
				break;
			case 3:
				block = block3;
				break;
			case 4:
				block = block4;
				break;
			}
		
		//Instantiate Block
		Instantiate(block, startingPoint.transform.position, startingPoint.transform.rotation);
		//setting active to true
		isSet = true;	
		}
		
	while(isSet)
		{
		if (zaehler > 1000) 
			{
			break;
			}
		//here is the moving script
		block.transform.Translate(0,Time.deltaTime,0,Space.World);
		zaehler++;
		Debug.Log(block.transform.position);
		}
	
	}

The debugger doesn’t show any error. The Debug.Log shows, that the transform.position is changing but the block is not moving.

Why not? I tried different versions (as mentioned in the reference) for the Translate script but none of them worked.

The while loop is in there cause I need it later.

Thanks for your help
mitti2000

The "block" variable doesn't seem to be set to anything. It's declared but never initialized. Also, http://forum.unity3d.com/threads/5105-Gravity-3d-Tetris?p=38267&viewfull=1#post38267

2 Answers

2

You need to assign the result of Instantiate to a variable (which refers to the new object), then move that, something like:

var blk = Instantiate (block, ......

and you shouldn’t do a ‘while’ like that, look at http://unity3d.com/support/documentation/ScriptReference/Transform.Translate.html
You move the block incrementally on each call to Update

You need to store the returned game object from Instantiate and perform your transformations on that - it looks like ‘block’ is your prefab, so doing any world space transforms on it are meaningless.

The docs describe it well: http://unity3d.com/support/documentation/ScriptReference/Object.Instantiate.html

“Clones the object original and returns the clone.”