I have a question hope you would be able to help me.
I did this script called CubeClass. Has a class called Cube, each cube object has an id, color...
I am stuck now and I do not know how to continue. I want to retreive the ID of the object whenever the player presses on it. For example cube number 6 will have an of 5. I have tried using OnMouseDown() but to no avail.
Hope you will be able to guide me to the right direction! Thanks in advance
here is my script:
var bl:GameObject;
var gr:GameObject;
var re:GameObject;
var cubecollection:GameObject[];
var colorcollection:String[];
var cubestorage:Array; //creates all instantiated cubes
static var ElementID7 : int = 0;
class Cube extends System.Object
{
var cprefab:GameObject;//since it is a prefab so it has to be of type GameObject
var id:int;
var cubecolor:String;
function Cube(cprefab:GameObject,id:int,cubecolor:String)
{
this.cprefab=cprefab;
this.id=id;
this.cubecolor=cubecolor;
}
}
function Start()
{
cubestorage = new Array();
createCubes();
drawCubes();
}
function Update () {
//bl.transform.position.x = 15;
Debug.Log("The seventh cube has: " + cubestorage[6].id);
Debug.Log("The seventh cube has color of: "+ cubestorage[6].cubecolor);
ElementID7 = cubestorage[6].id;
}
function OnMouseDown()
{
var cube:Object;
Debug.Log(cube.id);
}
function createCubes()
{
var cube:Object;
var counter:int =0;
var randomnumber:int =0;
for (i=0; i<4; i++)
{
for(j=0;j<4;j++)
{
randomnumber = Random.Range(0,3);
//Instantiate(cubecollection[Random.Range(0,3)],Vector2(i,j),Quaternion.identity);
cube = new Cube(cubecollection[randomnumber],counter++,colorcollection[randomnumber]);
cubestorage.Add(cube);
Debug.Log(cube.id);
}
}
}
function drawCubes()
{
var xcounter:int = 0;
var ycounter:float = 0;
var xgap:float=0;
//Debug.Log(cubestorage.length);
for(i=0;i<cubestorage.length;i++)
{
Instantiate(cubestorage*.cprefab,Vector2(xgap,ycounter),Quaternion.identity); //(prefab,position,rotation)*
*xcounter++;*
*xgap +=1.25;*
*if (!(xcounter%4))*
*{*
*xcounter=0;*
*ycounter+=1.5;*
*xgap =0;*
*}*
*}*
*}*
*```*