Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by pravin gate · Mar 28, 2011 at 12:00 PM · randomcubegenration

Random Genration of Cube

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Statement · Mar 28, 2011 at 12:10 PM

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); }

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pravin gate · Mar 28, 2011 at 12:17 PM 0
Share

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?

avatar image Statement · Mar 28, 2011 at 12:35 PM 0
Share

Well 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?

avatar image pravin gate · Mar 28, 2011 at 12:56 PM 0
Share

yah, 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...

avatar image Statement · Mar 28, 2011 at 02:06 PM 0
Share

Yeah 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 On$$anonymous$$ouseDown you can compare materials to see if it was the same object that was clicked.

avatar image
1

Answer by AngryOldMan · Mar 28, 2011 at 12:34 PM

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pravin gate · Mar 28, 2011 at 12:59 PM 0
Share

yes i just wanted to put them in random places after each time when u will play this game.

avatar image pravin gate · Mar 28, 2011 at 01:27 PM 0
Share

ok i'm trying to apply above code.bt may i'm doing in wrong way.

i'll just tel u wat i have done .

  1. have created a prefab (from maya). and in each scene(or say level) i'm placing number of cubes on scene from that prefab.

2.the i have created a empty gameobject.and attached a cube detection script. That means if i'm having 2 cubes in scene, and if i want to go to next level, i need to click on correct cube, and if u click correct cube , u will post to next level.(for choosing of correct cube i have used custom tag, and raycast concept).

avatar image pravin gate · Mar 28, 2011 at 01:32 PM 0
Share

3.now whenever i'll play this game i should feel that its a new game,as we r going to put each time random cubes(with different "materials"), so it will seems new game. so how should i use this code.?

avatar image AngryOldMan · Mar 28, 2011 at 03:59 PM 0
Share

don't it's example code and wont work properly, you should learn to code and use this as a guide to make your own script.

avatar image Abhinash kumar · Mar 21, 2013 at 08:04 AM 0
Share

thanks for these code it really was help full to me i have some more question. what if i want to move those random objects in the same range of x actually i want to create a bike game and i want random car in Terrain and i want to move those car in random manner what should i do plz help :(

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

How can I show a random integer on a cube 1 Answer

Random cubes 2 Answers

How can i destroy cubes that is randomly generated when i have passed them? 1 Answer

Random place changement 2 Answers

How to make an object/prefab choose between multiple given positions to spawn into? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges