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 starta · Apr 03, 2011 at 05:23 PM · transformrandom

random show objects

I creat the 30 empty gameobject in different places.

I have cube and sphere.

Start game, I want to randomly the 10 of empty gameobject were in place show cube or sphere.

(Cube and Sphere location are empty gameobject)

I know "random.range" this script.

But this can only set a range, and can't specify the location of objects.

What can i write the code? 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
0
Best Answer

Answer by DaveA · Apr 03, 2011 at 06:47 PM

You could do

pos :Vector3 = new Vector3 (Random.Range(-10.0, 10.0), Random.Range(-10.0, 10.0), Random.Range(-10.0, 10.0));

to make them show in random spots within the cubic volume -10,10 or look at 'insideUnitSphere' for example.

If you want to randomly choose one of your empty game objects:

var spots : Transform[]; // drag the objects from Hierarchy to Inspector to set these elements, something like this:

function Start()
{
   for (var i=0; i < 10; i++)
   {
     var spot = Random.Range(0,spots.Length);
     Instantiate (thing, spots[spot].position);
   }
}

You'll have to fix the syntax but it's close. Note that this lets you put as many or few empty objects into the array. Also it might result in more than one object appearing at the same spot. To avoid that, you could make an array of booleans like 'usedSpot' and set that when a spot is chosen, and check for it not being set before choosing another. Keep chosing random numbers until an unused one is found. Or you could look into a 'shuffle' algorithm, but this is getting beyond what I can get into right now.

Comment
Add comment · 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
0

Answer by starta · Apr 05, 2011 at 02:09 PM

Thank you

I use other guy's suggestion and his method is easy understand for me

I'm a beginner

As below:

I create cube and sphere into prefabs.

and create a script is called generateObjects.js:

static var objectCount:int=0;

and create another script is called generateObjects.js:

var cubePrefab:gameObject;//set these in the inspector
var spherePrefab:gameObject;//set these in the inspector
var haveAnObject:boolean=false;
function Update()
{
if(globalCount.objectCount<10 && !haveAnObject)
{
var rand=Random.Range(0,100);//to see if we should create an object
if(rand>50)
{
haveAnObject=true;
globalCount.objectCount +=1;
rand=Random.Range(0,100);//to see if we should make a cube or sphere
if(rand>50)
Instantiate (cubePrefab, transofrm.position, transform.rotation);
else
Instantiate (spherePrefab, transofrm.position, transform.rotation);
}
}
}

put generateObjects.js to all my empty objects.

Comment
Add comment · 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

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

No one has followed this question yet.

Related Questions

Generating a Displacement Map in Unity3d? 1 Answer

Teleport to a random height 1 Answer

Need help with Flashlight Script 1 Answer

keep an if statement updating per frame in function Update 1 Answer

Transform to a random height 0 Answers


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