Hii, i want to make random generation of cubes.I have number of n cubes with different materials applied to each.
Means suppose i 'm having 10 cubes in one scene, and in in second or in other scenes , i need to display random cubes from first scene . so how should i do that?
i'm a flash developer, so i know how to generate random images using following way.
Code:
function randomGen():String{
var labels=new Array("l1","l2","l3"); //(here que1,que2...etc are frames labels on which i have taken 3 images on each frame
var index:Number=Math.floor(Math.random() *labels.length);
return labels[index];
}
this.gotoAndStop(randomGen());
similar to this how can i generate random cubes in unity.?
**There is one common thing in all cubes is a custom tag. All cubes are having same custom tag.
Any help will be helpful.
Thanks.
2 Answers
2
I'm not quite sure what you mean by a random cube. There isn't many ways a cube can be random except materials applied to it? :) I don't know why you'd want the cubes from the first scene, is this some sort of palette? Maybe you can just make use of prefabs?
// In inspector, assign any cube prefabs here.
var cubePrefabs : GameObject[];
// Call this function to create a random cube.
function InstantiateRandomCube()
{
var prefab = cubePrefabs[Random.Range(0, cubePrefabs.Length)];
return Instantiate(prefab);
}
Similar to what Statment said, what do you mean by random?If you want ten randomly sized cubes in then something like
var CubePrefab : GameObject;
var AmountOfCubes : int;
function Update ()
{
if (AmountOfCubes <=10)
{
SpawnRandomCube();
}
}
function SpawnRandomCube()
{
var RandomCube = Instantiate (CubePrefab,transform.position,transform.rotation);
AmountOfCuces = AmountOfCubes + 1;
}
attach that to an empty gameobject where ever you want cubes to spawn, but they all spawn in the same place at the same time.then on the Cube~Prefab attach a script that says something like
function Awake ()
{
var SingleRandomSize = Random.Range (1,10);
tranform.localScale = Vector3 (SingleRandomSize,SingleRandomSize,SingleRandomSize);
}
or if you wanted them to spawn in random places then on your empty gameobject put
var CubePrefab : GameObject;
var AmountOfCubes : int;
var RandomPos : int;
function Update ()
{
if (AmountOfCubes <=10)
{
RandomPos = Random.Range (1,20)
SpawnRandomCube();
}
}
function SpawnRandomCube()
{
tranform.postition = Vector3 (RandomPos,RandomPos,RandomPos);
var RandomCube = Instantiate (CubePrefab,transform.position,transform.rotation);
AmountOfCuces = AmountOfCubes + 1;
}
if you want to be more specific about what you're trying to achieve you will get more specific help.
i mean to say i have number of n cubes with different materials applied to each . Actually i 'm trying to make a small memory game. each time when u will start game u will get random cubes, and u hav to remember these cubes to complete the level. in level u will show number of cubes , from that u hav to identify correct one. so wht will be the good way to achieve it?
– anon62947158Well I am not quite sure how you envision your game. But I'd start off by making prefabs for all the different types of cubes. Say you create a 5x5 grid of random cubes from these prefabs, show it for 60 seconds. Darken the screen for 30 seconds, then replace a few of them. When user clicks a cube that already was there, they get a penalty. When user clicks all cubes that are new, the next level loads. Something like that?
– Statementyah, similar to this, but the level should be lik this way: suppose for first level if u given a cube of white color.it will display for 30 sec. then u will get new screen and in that u will be shown 5 cubes, from that u just need to choose correct one cube(i.e) white color cube. and the next level will be loaded. and so on...
– anon62947158Yeah so you can start off with code supplied above. You could add code that creates a random prefab and store which prefab it was in a variable. Then in your OnMouseDown you can compare materials to see if it was the same object that was clicked.
– Statement