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 zero3growlithe · May 13, 2012 at 04:07 PM · loopreturnassignforbreak

How can I make my for loop "reset" if my array contains an value?

So having this loop below I have a simple problem... how can I make my loop to "try again" assigning gameObject to same place if an criteria is meet? (I've put return and break but i don't really know how to they work in general...)

 if (PlayerShipLoaded == true && ShipsSelected < 7)
 {
     var EnemyAIshipPlaces = GameObject.FindGameObjectsWithTag("EnemyShipPlace");
     for (place in EnemyAIshipPlaces)
     {
         RandomSelector = Random.Range (1,8);
         RandomSelectorString = RandomSelector.ToString();
     
         if (ArrayUtility.Contains (LoadedAIshipsBuiltin, RandomSelectorString)){
             return;
         } 
         else 
         {
             Instantiate (RandSelectorModelEquivalent, place.transform.position, place.transform.rotation);
             LoadedAIships.Add (RandomSelectorString);
             ShipsSelected ++;
             break;
         }
     }
 }
Comment
Add comment · Show 2
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 whydoidoit · May 13, 2012 at 05:26 PM 0
Share

Is your intention to allocate all of your ships in 1 of 8 random positions? As in there will be up to 8 ships and you want them to be in one of 8 different places after this code executes?

avatar image zero3growlithe · May 13, 2012 at 05:58 PM 0
Share

Allocate 8 ships randomly on 8 places without repeating them :)

5 Replies

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

Answer by Berenger · May 13, 2012 at 04:14 PM

Return will stop the function. Break will stop the loop. If you want to restart the loop, you need to use the iterator form of for : for( var i = 0; i < .... ) and reset i when you need to. That's a good way to have an infite loop though, so be carefull.

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 Piflik · May 13, 2012 at 06:07 PM 3
Share

When you fond a solution, post it. It might help later generations.

avatar image
1

Answer by Drakestar · May 13, 2012 at 04:22 PM

'Break' stops iteration over the loop. Any additional statements in the function block after this for loop are still executed. 'Return' ends the function call - no additional statements are executed, and the for loop iteration is automatically ended as a result. If there is no other code after the loop, both are accomplishing the same goal.

The exact behavior that you're trying to accomplish is a bit unclear to me. There's several variables in the code that aren't defined anywhere. Generally, you "try again" in a loop by either decrementing the loop counter, by breaking out and calling the same function again from the originating function, or by recursively calling the same function from within the loop.

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 zero3growlithe · May 13, 2012 at 04:39 PM 0
Share

What I want to do is to make this loop load for every found "EnemyShipPlace" a random gameObject which wasn't loaded earlier. btw. They are not defined because it is just part of my script as it is quiet big...

avatar image
1

Answer by whydoidoit · May 13, 2012 at 07:04 PM

Firstly - I can't see your code clearly for creating the ships, so I've written this answer using what I can see - which is the positions you want to allocate. You could easily reverse the logic if you can create a list or array of the ships you want to allocate.

The principle is that you create a set of the unique items, choose a random one and then remove it from the set.

Ok to allocate your ships in a unique list of positions, make sure that you have imported Systems.Collections.Generic into your script.

Then

var allocationList = new List.<GameObject>( EnemyAIShipPlaces );

This gives you a set of the location GameObjects, in a list which you can remove them from one at a time in random order.

Then loop for each of the ships you want to allocate and do this:

Get a random position:

 var positionIndex = Random.Range(0,allocationList.Count); 

Then get the game object position:

 var position = allocationList[positionIndex].transform.position;

And then remove the position selected from the list

 allocationList.RemoveAt(positionIndex);

That will have the benefit that it always runs only once for each position.

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

Answer by Piflik · May 13, 2012 at 04:13 PM

I am not sure I understand what you want to do, but if you want to run the same loop again inside the loop, you should make it a function, so you can call it recursively.

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

Answer by zero3growlithe · May 14, 2012 at 11:58 AM

Ok, so I've decided to avoid "for" loop as I couldn't check what is happening in it as Unity freezed after I've added one thing and pressed play xD Here's my self answer that works perfectly as i needed :P

EDIT: Because of ArrayUtility to be an editor function I had to make my own one so here's updated version of code:

 var AIshipsLoaded : boolean = false;
 private var RandomSelector : float = 0;
 var ShipsSelected : float = 0;
 var EnemyShipPlace : float = 0;
 var LoadedAIships = Array();
 private var RandSelectorModelEquivalent : GameObject;

 function Update(){
 if (PlayerShipLoaded == true && ShipsSelected < 7){
         var EnemyAIshipPlaces = GameObject.FindGameObjectsWithTag("EnemyShipPlace");
             RandomSelector = Random.Range (0,7);
             RandomSelectorString = RandomSelector.ToString();
 // Enemy Ships Loader=================================================================================================================
                 if (AIshipsLoaded == false && PlayerShipLoaded == true){
                     switch (RandomSelector){
                         case(0):
                             RandSelectorModelEquivalent = GXF362_Enemy;
                             break;
                         case(1):
                             RandSelectorModelEquivalent = CDistra_Enemy;
                             break;
                         case(2):
                             RandSelectorModelEquivalent = CERN_Enemy;
                             break;
                         case(3):
                             RandSelectorModelEquivalent = Amphithere_Enemy;
                             break;
                         case(4):
                             RandSelectorModelEquivalent = Solaris_Enemy;
                             break;
                         case(5):
                             RandSelectorModelEquivalent = Zepher_Enemy;
                             break;
                         case(6):
                             RandSelectorModelEquivalent = Interceptor_Enemy;
                             break;
                         case(7):
                             RandSelectorModelEquivalent = Helios_Enemy;
                             break;
                     }
                 }
             if (ArrayUtility() == false){
                 Instantiate (RandSelectorModelEquivalent, EnemyAIshipPlaces[EnemyShipPlace].transform.position, EnemyAIshipPlaces[EnemyShipPlace].transform.rotation);
                 LoadedAIships.Add (RandomSelectorString);
                 EnemyShipPlace ++;
                 ShipsSelected ++;
             }
             print (LoadedAIships);
     }
 }

 function ArrayUtility (){
     for (i = 0; i <= LoadedAIships.length - 1; i++){
         var LoadedAIshipsBuiltin : String[] = LoadedAIships.ToBuiltin(String);
         RandomSelectorString = RandomSelector.ToString();
         if (LoadedAIshipsBuiltin[i] == RandomSelectorString){
             return true;
         }
     }
 }
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

8 People are following this question.

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

Related Questions

Stopping A Loop - Return or Break? 2 Answers

Is having a "break" in a "foreach" loop that is itself inside a "for" loop breaking both loops? 3 Answers

C# List For Loop Only Returns Last Element 0 Answers

list.contains isn`t working 1 Answer

Is there a way to break out of a function and return control to another script's function? 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