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
0
Question by xToxicInferno · Feb 15, 2010 at 04:29 AM · instantiateloop

Is there a way to call up a IF statement like a function?

In a script i wrote, i have it so that when the OnTriggerEnter function is called, it will run, do a IF statement to figure out what activated the Trigger, and then do the action it is expected to do depending on what triggered it. I have it set so that when the Trigger is activated it will spawn some enemies. I wanted to make this script universal so i made it so that if it was triggered, it would spawn however many enemies the user set it as, in the spots the user wanted it at.

var spawnobject : Transform; var spawnspot : Transform[]; var numberofspawnspots : int =3; private var maxspots : int = 0;

function OnTriggerEnter (other : Collider) { if (other.gameObject.tag == "Player" ) { var numberspots = maxspots; Instantiate(spawnobject, spawnspot[numberspots].transform.position, Quaternion.identity); if (numberspots < numberofspawnspots) { if (numberspots == numberofspawnspots--){ destroy(); } numberspots++; maxspots++; print(numberspots); return; } destroy(); } return; }

function destroy (){ Destroy(this, 0.0); }

Now that you see the code, it is easier to explain. I have it so that it will run through the code if the object is tagged "Player". I did this so when i shoot the bullet doesn't trigger it. Now the problem is, i have it so it returns after every check. Return sends it back to the top of the script so it has to start the process over again, which means i have to retriggger it, which isn't what i want. I want so it will just return to the IF statement. Is there anyway to do that?

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

3 Replies

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

Answer by Sebas · Feb 15, 2010 at 05:03 AM

It sounds like you want to spawn multiple enemies. The current script most likely only spawns a single one; that's why you want the if statement to occur multiple times. That sounds like a good spot for a loop, like Leigh suggested. Check out a while loop. It will cycle through your method while a certain condition is met. Your condition would probably be that the current number of enemies is less than your specified number of enemies to be spawned. You would have to restructure your if-statements and substitute for a while loop.

I simplified a little bit, because I'm unsure of your use of all variables, but this should give you a hint of how to solve your problem:

var numberofEnemiesToBeSpawned : int = 3; private var spawnedEnemies : int;

function OnTriggerEnter (other : Collider) { if (other.gameObject.tag == "Player" ) { while (spawnedEnemies < numberofEnemiesToBeSpawned) { //instantiate one enemy spawnedEnemies++; } } }

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 xToxicInferno · Feb 15, 2010 at 05:12 AM 0
Share

Thanks so much, i didn't even know of the existence of the while command. It seems to me it is going to be very useful in the future! Thanks again!

avatar image
1

Answer by Leigh · Feb 15, 2010 at 04:43 AM

I'm a little unclear on exactly what you want to do.

OnTriggerEnter will occur once when the collision starts, and return will exit the method completely (ie. another collision event will have to occur before that method runs again.)

Are you wanting a loop to occur inside your method to spawn multiple GameObjects?

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 xToxicInferno · Feb 15, 2010 at 05:00 AM 0
Share

I want it so it will spawn all the objects at the same time, rather then having to enter and exit and reenter however many times. So in other words i want it so that it will spawn all the ones it needs to at one time, and without me adding a function for every single one, as i know how to do that, but it would be me adding 100+ functions for a simple thing like this.

avatar image
0

Answer by Brian-Kehrer · Feb 15, 2010 at 05:01 AM

It's not very clear what you want, but it sounds like you want 'else' or 'switch'

if (x){
}
else if(y){
}
else{
}
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

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

Why is there a difference between script and in-game sequence of a script loop? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Help With Add and Remove on a loop 2 Answers

Loop through textures every half second 2 Answers

Cycle Through All GameObjects in the Scene 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