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
1
Question by Greg 9 · Jan 27, 2012 at 12:27 PM · randomspawnfunctionmultiplecall

Call a function multiple times

Hi, I'm wondering if there is something I'm missing here.

Is there a fast, and easy way to call my function "Spawn" from the start menu 3 times (using the amount variable)?

Seems like this should be obvious but I can't figure it out. Here's the code.

//Objects To Spawn

var objectsToSpawn : GameObject[];

//Spawn Locations

var spawnLocations : Transform[];

//spawn amount

var amount = 3;

function Start() {

 Spawn(amount);

}

function Spawn() {

//Select From Objects To Spawn

var thingToSpawn : int = Random.Range( 0, objectsToSpawn.length );

//Select From Spawn Locations

var placeToSpawn : int = Random.Range( 0, spawnLocations.length );

Instantiate( objectsToSpawn[thingToSpawn],

spawnLocations[placeToSpawn].position,transform.rotation);

}

I'm trying to call the spawn function 3 times. Any ideas where I can stick the var amount (if anywhere)?

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
6
Best Answer

Answer by BiG · Jan 27, 2012 at 12:34 PM

This is how your Start function should be:

var amount = 0;

function Start(){ for (amount=0; amount<3; amount++) Spawn(); }

Watch out: I cannot say if the rest of your code is correct (even because you have not formatted it properly): this is simply the logic behind calling a function 3 times as the application starts.

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 Greg 9 · Jan 27, 2012 at 12:42 PM 1
Share

$$anonymous$$any thanks, The spawn function works fine. It basically selects (randomly) between several spawn locations, then selects (again, randomly) between several prefabs and then instantiates the prefab at the position.

It's a nice way to make a cheap L4D director style item placement. Again, thanks for this, much appreciated

avatar image
6

Answer by save · Jan 27, 2012 at 12:38 PM

You can use a loop to iterate through the amount,

function Spawn (quantity : int) {

 for (var i : int = 0; i&lt;quantity; i++) {
     //Select From Objects To Spawn
     var thingToSpawn : int = Random.Range( 0, objectsToSpawn.length );
     //Select From Spawn Locations
     var placeToSpawn : int = Random.Range( 0, spawnLocations.length );

     Instantiate( objectsToSpawn[thingToSpawn], spawnLocations[placeToSpawn].position,transform.rotation);
 }

}

You could also pass more variables to the function, to switch objects or so:

function Spawn (quantity : int, thing : GameObject) {}
Comment
Add comment · Show 3 · 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 BiG · Jan 27, 2012 at 12:49 PM 1
Share

I have just ninja'd you, @save! ;) but your explanation is much more complete, with respect to the OP's request.

+1

avatar image save · Jan 27, 2012 at 12:55 PM 1
Share

Haha yes you did. :) You'll get a Fridays +1!

avatar image BiG · Jan 27, 2012 at 12:57 PM 0
Share

$$anonymous$$any 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

6 People are following this question.

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

Related Questions

Choosing random function 3 Answers

AI spawning areas. What are the ways to make them? 1 Answer

Spawn object from Random Vector3 in array 0 Answers

How to detect Mouse over gui and guilayout elements? 4 Answers

Endless/infinite runner random enemy spawning multi lane 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