instantiating multiple objects and assigning them individual values

I’m trying to instantiate 16 objects (in a grid 4x4) and assign each object a value and ID dependent on the random number that is generated when the object is instantiated.

The code below is present on each prefab. The timer on the same object calls the function below and it generates the number to assign a texture to that object, id and value.

var cube : GameObject;

function SwitchBlock(){
	setupBoard.randomNumber = Random.Range(0,setupGame.textureArray.length);  
    cube = Instantiate(cube, transform.position, transform.rotation);
   	cube.renderer.material.mainTexture = 
    	Resources.Load("Textures/" + setupGame.textureArray[setupBoard.randomNumber], Texture2D); 
	this.GetComponent(setLetter).points = setupGame.setLetters[setupBoard.randomNumber,1];
	this.GetComponent(setLetter).id = setupGame.setLetters[setupBoard.randomNumber,0];
	print(cube.GetComponent(setLetter).points);
}

The problem that I am having is that when I click on any block that’s instantiated, I get the value and ID of the last block only. I have printed out when all values are assigned so I know that the blocks are getting different values set, but they seem to be getting over written some where.

I think it is because I am instantiating the objects all called the same name, but I’m a beginner and not 100% certain.

Can anyone tell me my mistake? And is there a more efficient way to do this?

Thanks

Are you running this as a calling this method from either your setupGame or setupBoard scripts? Because you don’t appear to be making this a coroutine of Update. Also, I believe you should be instantiating as an instance of the prefab … so as in a new variable. This may be why you’re getting the same values. Anyway, this is how I would do it:

var cube : GameObject;

function SwitchBlock(){
	
    setupBoard.randomNumber = Random.Range(0, setupGame.textureArray.length);  
    
    var cubeInst : GameObject = Instantiate(cube, transform.position, transform.rotation) as GameObject;
    cube.renderer.material.mainTexture = Resources.Load("Textures/" + setupGame.textureArray[setupBoard.randomNumber], Texture2D); 
    cubeInst.GetComponent(setLetter).points = setupGame.setLetters[setupBoard.randomNumber, 1];
    cubeInst.GetComponent(setLetter).id = setupGame.setLetters[setupBoard.randomNumber,0];
    
    print(cubeInst.GetComponent(setLetter).points);
    
}

I believe that should work.

Hope that helps, Klep

Hey, thanks for the help but unforutnately it hasn’t worked :frowning:

I’m calling the above from this (which is placed above the code I asked about before):

var switchTime = 3;
var seconds = switchTime;

function Start () {
    InvokeRepeating ("Countdown", 1.0, 1.0);
}

function Countdown () {
    seconds -= 1;
    if (seconds == 0){
    
    	if (gameTimer.timer == 0){
    		CancelInvoke("Countdown");
    		gameTimer.timer = 30;
	    	Application.LoadLevel ("mainMenu");
       	}
    	else {
    		seconds = switchTime;
    		Destroy(gameObject);
			SwitchBlock();
    	}
    }
}

I’m not calling this within an update function as the start function calls the countdown timer which in turn calls the switchBlock function.

I’m kind of stuck now. Any other advice?