There are many objects with the names "TileLeft", "TileTop" you need to get the position and the name of the last one with isKinematic enabled.
- Last on the list? Or the one with which isKinematic was included last? - M. Green
- which will be included isKinematic first, but from the end of the list you need to sort through - Kill Noise
- let's get better in order. You have a sheet with links to objects. These objects contain a rigidbody. Do you need to get from this list, passing from the end to the beginning of the last object, in which .isKinematic = true? - M. Green
- yes, that's right they wrote - Kill Noise
- I will come home - I will write an algorithm) - M. Green
1 answer
Stack<GameObject> _stack; void GameObject GetLastKinematic() { GameObject cashObj; var array = _stack.ToArray(); int count = array.Length; for (int i = count - 1; i >= 0; i--) { var obj = array[i]; if (obj.activeSelf) { cashObj = obj; } } return cashObj; } Those. we declare a reference variable on GameObject , and then we start sorting the list from the end. Every time we find an isKinematic object, we assign it to a variable, and finally, after the crawl ends, we will have the link to the last of the objects that have this flag.
However, I do not quite understand what is the meaning of the traversal from the end? After all, when traversing from the beginning, the first isKinematic object encountered will be the last at traversing from the end. In fact, when passing from the end, we need to check each object at each inspection, and when passing from the beginning, there is a possibility of reducing the number of inspections very much.
And if you are satisfied with the search from the beginning and nothing more is needed to get the object, then you can generally use the Predicate and reduce the code to one line.
using System.Linq; Stack<GameObject> _stack; void GameObject GetLastKinematic() { var list = Stack.ToList(); return _list.Find(obj => obj .activeSelf); } In this case, the search will be performed on the sheet of the first instance of the class that satisfies the conditions in the brackets. But to turn a stack into a sheet, you will need to use Linq.
- Sori, I forgot to clarify, I have two Stack 'and "LeftTiles" and "TopTiles" I have code that renames for example from "TileTop (Clone)" to "TileTop" and somehow the stack turns out to be wrapped. Because you need to sort out the bottom. I want to implement the teleportation on the tile that has not yet fallen, those that fall they are disabled. - Kill Noise
- I apologize again, you need to check not
IsKinematicbut whether the object is active - Kill Noise - one@Kill Noise, why do you need to stack there? Why can't I use a regular sheet? A renaming process does not affect. Code corrected from IsKinematic to whether the object is active - M. Green
- @Kill Noise, corrected under work with a stack. But, again, you can go from the beginning through the array. - M. Green
- If you go first the final result will be different? - Kill Noise