Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
-1
Question by Zitoox · Oct 05, 2016 at 05:26 AM · script.

Ramdomly spawn object

Could someone help me on making a script that ramdomly spawn a few objects? I am not that good at this.

I wanted to make a spawn script that would allow me to spawn a ramdom prefab that i've chosen in a ramdom location. For an example: I have a folder with 6 types of birds, and i want the script to spawn one of them in a random location from time to time.

How do i make the part of spawning a random game object of my chose? It wouldn't be a good thing if i saw a plant spawning in my front suddenly...

By using C# and Javascript you can create a variable with a fixed type of object. With this, i can spawn that object. But i didn't understand the part of spawning different objects.

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 Namey5 · Oct 05, 2016 at 07:07 AM

Well, where you would normally have a GameObject, you could use an array, and then instantiate that with a random number, i.e.

 //in C#, because everyone seems to use it
 
 public GameObject[] objects;    //In the inspector you can set the size of this value and drag however many objects you want in here
 
 //and then wherever you are spawning an object
 
 int i = Random.Range (0, objects.Length);
 Instantiate (objects[i], objects[i].transform.position, Quaternion.identity);    //Use whatever random position/rotation you want here in the last 2 function variables
Comment
Add comment · Show 1 · 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 Zitoox · Oct 05, 2016 at 11:23 PM 0
Share

Nice! The script is almost finished.

avatar image
1

Answer by Kossuranta · Oct 05, 2016 at 07:21 AM

Make an array of game objects that you want to spawn and then define min and max positions in the game world for them. Then just spawn them using Random.Range.

 public GameObject[] objects;
 public Vector3 minPos;
 public Vector3 maxPos;
     
 void Update()
 {
     Vector3 position = new Vector3(Random.Range(minPos.x, maxPos.x), Random.Range(minPos.y, maxPos.y), Random.Range(minPos.z,maxPos.z));
     Instantiate(objects[Random.Range(0, objects.Length)], position, 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 Namey5 · Oct 05, 2016 at 08:02 AM 2
Share

Random.Range always returns at least 1 below the max value, so you shouldn't really subtract 1 from the max otherwise it will never spawn the last object.

avatar image Kossuranta Namey5 · Oct 05, 2016 at 01:20 PM 0
Share

Oh right it's exclusive, thanks :)

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

58 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Does c# scripts take up more space than javascript scripts? 1 Answer

How can i get original Pixel size of texture in C# script? 0 Answers

Pinpointing one vertice with RaycastHit 3 Answers

When the enemy move between waypoints if a waypoint is too big he will stuck on it how can i solve it ? 0 Answers

How can i put a gameobject in bottom of game screen as selecteable item ? 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