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 dendens2 · Apr 06, 2013 at 11:27 PM · javascriptinstantiate

Instantiate position from four options.

Hi, so I have to have zombies spawn at one of four spawners. My problem is that I don't know how to put in more than one location as the spawn locations. I tried doing

 Instantiate(Zombie, (spawn1 || spawn2 || spawn3 || spawn4), etc...)

but that did not work. Is there any other way I can do this?

Comment
Add comment · Show 1
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 Unitraxx · Apr 07, 2013 at 01:38 AM 0
Share

I like how you think, but unfortunately OR works with boolean arguments and thus are those spawnpoints converted to true/false.

1 Reply

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

Answer by Lockstep · Apr 07, 2013 at 12:09 AM

This is quite simple just make an array which contains your possible spawn locations and pick one randomly. The code could look like this (c#):

 public Transform[] spawnPoints;  //assign the spawnpoints in the inspector or during Start
 
 void SpawnZombie(){
     int pick = Random.Range(0, spawnpoints.Length);  //select one spawnpoint randomly
     Instantiate (Zombie, spawnPoints[pick].position, spawnPoints[pick].rotation);
 }
Comment
Add comment · Show 6 · 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 dendens2 · Apr 07, 2013 at 12:56 AM 0
Share
 var spawnPosition = new Array (spawnOne.transform.position, spawnTwo.transform.position, spawnThree.transform.position, spawnFour.transform.position);
             
         
         
             Instantiate(zombie, (spawnPosition[Random.Range(0, 3)]) + Vector3((Random.Range (0, spawnOne.renderer.bounds.size.x)) / 2, 0.5, (Random.Range (0, spawnOne.renderer.bounds.size.x)) / 2), Quaternion.Euler(0,0,0));
 
 


For some reason in the Instantiate it is telling me that the

 (spawnPosition[Random.Range(0, 3)])

is an object and won't compile because it says that Object + Vector 3 is not allowed.

avatar image Lockstep · Apr 07, 2013 at 01:27 AM 0
Share

the js Array class can store any object, thus spawnPosition[x] is a general object. You need to cast it to Vector3 if you want to use the Array class

 Instantiate(zombie, ( (Vector3) spawnPosition[Random.Range(0, 4)]) + Vector3(((Random.Range (0, spawnOne.renderer.bounds.size.x)) / 2, 0.5, (Random.Range (0, spawnOne.renderer.bounds.size.x)) / 2), Quaternion.identity);

I'd still recomend the builtin array in my example since you have better controll over the type. The cast will fail if you accidentially add an object to the array which is not a Vector3. The sysntax would be:

 var spawnPoints : Vector3[];
 
 function Start(){
      spawnPoints = [spawnOne.transform.position, etc];
 }

There is also a page on different arrays on the wiki.

Also take note, that the right number of Random.Range is exclusive if you use integers. Thus you need Random.Range(0,4) or better Random.Range(0, spawnPosition.length)

avatar image Unitraxx · Apr 07, 2013 at 01:36 AM 0
Share

I think his only fault were the parenthesis around spawnPosition[Random.Range(0, 3)]. Try removing those.

avatar image Lockstep · Apr 07, 2013 at 01:51 AM 0
Share

I don't see why those would hurt.

avatar image dendens2 · Apr 07, 2013 at 03:39 AM 0
Share

Thank you!

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

12 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

Related Questions

How to correctly convert Object to GameObject 1 Answer

Please help with this code!! 1 Answer

Getting a component from an instantiated prefab. 1 Answer

Update call for Multiple GUIText fails 1 Answer

How can I declare class properties of an object in a single line? 2 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