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 Allie · Jun 23, 2013 at 08:17 AM · instantiaterandomarrayscursor

How do you spawn a random object from an array.

I'm trying to create an array with a list of objects. What I want to do is randomly select one of these objects and spawn it at the point my cursor is. Any ideas?

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

Answer by Em3rgency · Jun 23, 2013 at 09:54 AM

 Random rnd = new Random();
 int myRandom = rnd.Next(1, 13); // will select a (pseudo)random number between 1 and 12

Just use this like rnd.Next(0,yourArraySize) and spawn the object with the index it spits out.

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 Nanity · Jun 23, 2013 at 09:19 AM

The curser is a 2D object on a 3D space, so I assume you want object to spawn where the camera is looking at instead of the camera position (not tested and not refined like testing if the objects fits there):

 // Your array
 public Transform[] objectList;

 //However and wherever you determinate the mouse click
 Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 RaycastHit hit;
 if (Physics.Raycast (ray, out hit, 100))
 {
     // Your code to instantiate
     int random = (int)Mathf.Floor(Random.Range(0, objectList.Length));
     Instantiate (objectList[random], hit.point, Quaternion.identity);
 }
Comment
Add comment · Show 2 · 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 Em3rgency · Jun 23, 2013 at 09:23 AM 1
Share

Shouldn't that be Instantiate (objectList[random], hit.point, Quaternion.identity); ?

avatar image Nanity · Jun 23, 2013 at 03:34 PM 0
Share

Oh yes, copy paste is tricky ;)

Edit1: And found out that the maximum is excluded from the pool, so I removed the check. ( http://docs.unity3d.com/Documentation/ScriptReference/Random.Range.html )

Edit2: (int) typecast floors the float by default, so you could drop the $$anonymous$$athf.Floor(), too.

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

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

can you instantiate random prefabs from the resource folder? 2 Answers

Three Spots For Three Random Objects 1 Answer

Instantiate random object from Array 2 Answers

Three Random Objects From Array Without Repeat 1 Answer

Instantiate a random item at a random position 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