How to access a variable after randomly selecting it from an array

Ok,

script…

#pragma strict

var player : Transform;

var blockArray = new Array(blockNormal, blockCollapse, blockRocks, blockCart);
var blockNormal : Transform;
var blockCollapse : Transform;
var blockRocks : Transform;
var blockCart : Transform;

InvokeRepeating("PlaceBlock",0,2);

function PlaceBlock()
{
blockArray[Random.Range(0, blockArray.Length)];
}

So, I am trying to create a temple-run style script. At the moment there are no corners, so I’m alright on that front. The way I am planning to move the ‘blocks’ is to get the players position, and move it x distance in front of that. If there is a better way, tell me.

So I have the random selecting of the prefabs done, but after I have randomly selected it, how do I access it? Usually I’d go:

blockNormal.transform.position.x = player.transform.position.x + 5

But how do I do that after a:

blockArray[Random.Range(0, blockArray.Length)];

Help?

Ok,

Got it working. It still doesn’t generate in front of the player though – whats wrong?

#pragma strict

var player : Transform;
var randBlock : Transform;
private var groundOffset : int  = 2;

var blockArray = new Array(blockNormal, blockCollapse, blockRocks, blockCart);
var blockNormal : Transform;
var blockCollapse : Transform;
var blockRocks : Transform;
var blockCart : Transform;

InvokeRepeating("PlaceBlock",0,2);

function Start()
{
    Instantiate (blockNormal);
    Instantiate (blockCollapse);
	Instantiate (blockRocks);
	Instantiate (blockCart);
}

function Update()
{
blockNormal.transform.position.y = groundOffset;
blockCollapse.transform.position.y = groundOffset;
blockRocks.transform.position.y = groundOffset;
blockCart.transform.position.y = groundOffset;
}

function PlaceBlock()
{
var randBlock : Transform = blockArray[Random.Range(0, blockArray.length)];

randBlock.transform.position.z = player.transform.position.z + 3;
)

The update function (Should, but isn’t) make them hover at 2 units off the ground, function PlaceBlock (Should, but isn’t), place a random block in from of each other. All it does is loop through all of them, and stop, along the x axis, not z. Help?

Why not store the random number you generated in an int, and acces the blocks with that index in your array instead?

Also, try to avoid the Array class. Use the built-in arrays instead, like so:

var blockArray : Transform[];

not completely sure on the syntax of it ( I’m just writing this from the top of my head, I use C# myself ) but this is alot faster than the Array class.

And why do you store references 2 times? Just store the references in your array and get rid of the seperate ones. With the built-in array you can also drag drop your objects in the inspector if you need to

All of your code doesn’t make sense…

var player : Transform;
var randBlock : Transform; //why define this here?
private var groundOffset : int  = 2;
 
//you create the array before even defining the variables that you put in said array at creation
var blockArray = new Array(blockNormal, blockCollapse, blockRocks, blockCart);
var blockNormal : Transform;
var blockCollapse : Transform;
var blockRocks : Transform;
var blockCart : Transform;

InvokeRepeating("PlaceBlock",0,2);

function Start()
{
    //those variables you put in the array aren't even instantiated until here... how did they even end up in the array?
    Instantiate (blockNormal);
    Instantiate (blockCollapse);
    Instantiate (blockRocks);
    Instantiate (blockCart);
    //also note, instantiate creates a NEW instance from the prefab passed in
    //you aren't grabbing refs to the new instances that are returned by 'Instantiate'
}

function Update()
{
//this updated the y position on the prefab, NOT on the instances in game
blockNormal.transform.position.y = groundOffset;
blockCollapse.transform.position.y = groundOffset;
blockRocks.transform.position.y = groundOffset;
blockCart.transform.position.y = groundOffset;
}

//when and where is this method called?
function PlaceBlock()
{
//you already defined randBlock at the top... pick one!
var randBlock : Transform = blockArray[Random.Range(0, blockArray.length)];

//randBlock is already typed as a transform
//why are you accessing the transform property of a transform?
//also you can say += instead of this, randBlock.transform.position.z += 3;
randBlock.transform.position.z = player.transform.position.z + 3;

//and again, this randBlock appears to be the prefab, not an instance
)