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 Tyler 2 · Nov 28, 2010 at 02:21 AM · instantiatevector3random

Random instantiate?

Hello. I am trying to make a script where there are multiple possible object variants to spawn (in this case enemy types) and the it randomly spawns a set number of them at repeating intervals, but when I try my script I get the error " No appropriate version of 'UnityEngine.Random.Range' for the argument list '(UnityEngine.GameObject, UnityEngine.GameObject, UnityEngine.GameObject)' was found."

How can I fix my script? Thanks :)

var A : GameObject; var B : GameObject; var C : GameObject;

if (Projectile.Defeat < 200 && Projectile.Defeat > 80) { yield WaitForSeconds(5.0); Instantiate(Random.Range(A,B,C), position, Quaternion.identity); }

EDIT For being organized, is there a better way to instantiate multiple objects other than copying the instantiate part of the script like this?

   Instantiate(objs[(Random.Range(0, 3))], new Vector3(Random.Range(481.5735, 559.3441), -791.811, Random.Range(380.1254, 420.0663)), Quaternion.identity);
    Instantiate(objs[(Random.Range(0, 3))], new Vector3(Random.Range(481.5735, 559.3441), -791.811, Random.Range(380.1254, 420.0663)), Quaternion.identity);
   Instantiate(objs[(Random.Range(0, 3))], new Vector3(Random.Range(481.5735, 559.3441), -791.811, Random.Range(380.1254, 420.0663)), Quaternion.identity);
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

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Zib Redlektab · Nov 28, 2010 at 03:24 AM

You could make an array of GameObjects instead, get a random number between 0-3, and use it as the index, I think.

var objs : GameObject[];

// Populate array with objects A, B, and C here

if (Projectile.Defeat < 200 && Projectile.Defeat > 80) { yield WaitForSeconds(5.0); Instantiate(objs[(Random.Range(0, objs.Length))], position, Quaternion.identity); }

To do a random position as well, you could replace 'position' above with a random Vector3, within a range you decide (xMin & Max, etc):

new Vector3(Random.Range(xMin, xMax), Random.Range(yMin, yMax), Random.Range(zMin, zMax))

And to create more than one object (3 objects, for example) :

...
yield WaitForSeconds(5.0);
for (var i = 0; i < 3; i++) {
    Instantiate(objs[(Random.Range(0, objs.Length))], position, Quaternion.identity);
}
Comment
Add comment · Show 12 · 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 Eric5h5 · Nov 28, 2010 at 03:44 AM 1
Share

You should use "`var objs : GameObject[];`" ins$$anonymous$$d of Array, remove $$anonymous$$athf.Round, and use "`Random.Range(0, objs.Length)`" ins$$anonymous$$d of hard-coding the upper range

avatar image Tyler 2 · Nov 28, 2010 at 05:03 AM 0
Share

Noob question, but how do I "Populate array with objects A, B, and C"

avatar image Zib Redlektab · Nov 28, 2010 at 05:41 AM 0
Share

If you used my code above, you should now have an Array in the inspector called objs, and you can fill it there. Otherwise, just use objs[0] =

avatar image Tyler 2 · Nov 28, 2010 at 06:48 AM 0
Share

$$anonymous$$y bad, I guess I should try a script before saying "I have no idea what that means" :). Anyways, thanks for your help Zib and Eric.

avatar image Tyler 2 · Nov 28, 2010 at 06:57 AM 0
Share

Ive run into another problem, is there any way to instantiate the objects at different points within the position range? I tried copying the instantiate part of the script and although the objects spawned are different, they all spawn at the same place.

Show more comments

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

Randomly position Instantiated GameObject's 1 Answer

Spawning objects at random Vector3 3 Answers

Need help instantiating enemies between random locations 1 Answer

How would i use a 2D array for level generation 0 Answers

Get rotation from hit object 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