- Home /
Using animations in prefabs
I have a floor made out of cubes. And when the player moves around, random boxes around him animate and scale up. I'm trying to do this with a prefab. The problem I'm facing is that, when the player moves around, the animation works. But it always happens to the first cube (the first prefab I created). How can I have that animation for the random cube that's been selected and not the first cube always?
This is roughly what I'm doing:
var tileArray : GameObject[];
var nodes : Array;
var speed : float = 5;
var x : float;
var z : float;
var tile : GameObject;
function Start ()
{
InvokeRepeating("LiftRandomTile", 0, 1);
}
function Update () {
x = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
z = Input.GetAxis("Vertical") * speed * Time.deltaTime;
transform.Translate(x, 0, z);
}
function OnTriggerEnter (other : Collider) {
tileArray += [other.gameObject];
}
function LiftRandomTile () {
if (tileArray.Length > 0)
{
Debug.Log("Nodes " + nodes);
tile = tileArray[0];
//tile.transform.position.y += 5;
tile.animation.Play("Floor Animation");
}
tileArray = new Array();
}
Thanks.
Answer by HarshadK · Sep 26, 2014 at 09:54 AM
In your LiftRandomTile() you are always assigning your first tile by assigning first element from tileArray as:
tile = tileArray[0];
Change it to:
tile = tileArray[Random.Range(0,tileArray.Length)];
@Harshad$$anonymous$$ that's only for the cubes around the character (I have a box collider under the character that works as a trigger). The problem is that the animation only occurs to the first cube and not the cube around the player. I tried destroying the said cube and that works. But the animation is not applied to that cube.