- Home /
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);
}
}</pre>
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
You could make it all so much easier, placing all your blocks in an array, also randomObject = $$anonymous$$athf.Round(Random.Range(1.0,4.0)); does not need the rounding, randomObject is an integer so it drops after the decimal. Then you don't really need the switch then, you can simply instantiate the object with the defined prefab in the array with arr[randomObject]. Also, to instantiate smg it is
var smg =Instantiate(arr[randomObject], Vector3, rotation);
Personal point of view, I would create a separate script attached to the created object ins$$anonymous$$d of moving it from this one. It could be easier to read and debug the whole thing later on.
Answer by DaveA · Mar 18, 2012 at 08:18 PM
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
Answer by davedx · Mar 19, 2012 at 05:40 AM
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."
Your answer
Follow this Question
Related Questions
How do I duplicate an object to work with a shootemup 0 Answers
Accessing prefabs 1 Answer
ReplacePrefab() resets transforms for instanced prefabs? 1 Answer
Bullet not always shooting in forward direction. Code included. 2 Answers
Howto set main.camera as a parent when main.camera itself is a child of a prefab 1 Answer