remove object from loop once criteria is met...?

NOTE:

  • I am just cracking open Unity and am working in a Win7 environment w/ Unity 5.4.3f1 and Mono 5.9.6
  • also, it looks like my code formatting got lost in the post - is there a proper way to post scripts?

I have a group of four objects that I am changing focus on by using the arrow keys.
e.g.: R-arrow cycles from obj0 > obj1 > obj2> obj3 > obj1 … and so on. L-arrow does the same in reverse.

void GetObjLeft() {
if (selectedObjPositon == 0) {
selectedObjPositon = 3;
SelectObj (Obj [3]);
} else {
selectedObjPositon – ;
SelectObj (Obj [selectedObjPositon]);
}
}

void GetObjRight() {
if (selectedObjPositon == 3) {
selectedObjPositon = 0;
SelectObj (Obj [0]);
} else {
selectedObjPositon ++ ;
SelectObj (Obj [selectedObjPositon]);
}
}

Once one of the objects has met a certain criteria [moving more than 4 on the z aixs] I want it to be removed from the loop so I don’t need to hit the arrow keys multiple times to skip over the missing asset. I thought I could just reassign it’s selectedObjPostion to something outside of the loop (like 50), but that just kills the index.

I got as far as:
if (selectedObj.transform.position.z > 4) {
removeObjFromLoop();
}
but I’m stuck on the function for “removeObjFromLoop”

–thanks in advance for any tips.

I would recommend using a list for this purpose. You can iterate over the list itself then remove the object from the list once you have detected that that object has satisfied its criteria.

The roguelike 2d tutorial used this in level generation to remove coordinates after they had been allocated. You could check that tutorial out if you wanted to see how that might work.

1 Like

Thanks for the quick response!

Are you referring to this: 2D Roguelike - Unity Learn ?

I’ll look into it.

1 Like

To clarify what @absolute_disgrace was saying, Lists are a class in System.Collections.Generic, so you’ll have to add a using statement at the top of your script to import the List class, assuming you’re using C#.

You can also post code snippets using the code tags, under insert > code.

(code=CSharp)Like this.(/code)

Where () should be [ ]

1 Like

Ahhh thanks @Scabbage for the clarification and the code snippets syntax - I tried derp but ya… that didn’t work.

Yep you got the right tutorial bubbafrye. If you want to insert code there is a button on the inputbox to help out. The code tags are square brackets instead of those angeled ones.

(code=CSharp)Somecode = new CodeSnippet();(/code)
‘’ Replace the () with [ ] for the start and end tags.