do...while with Transforms?
I'm attempting to create a framework wherein I can move an object, regardless of type, along a set path defined by an n-dimensional array of path waypoints.
In the following method, _currentTarget
will always be the next waypoint (index) in the array, or the previous point if we've reached points.Length
(this is obviously not currently implemented, I just mention it for context).
As written, this method returns the error: Operator '<' cannot be applied to operands of type 'Transform' and 'int'
void MoveObject(Transform[] points)
{
MovableObject.transform.position = Vector3.MoveTowards(MovableObject.transform.position, _currentTarget, MoveSpeed*Time.deltaTime);
var i = 0;
do
{
// Move the object
} while (points[i] < points.Length);
}
I've found out that Array.Length
returns not the number of indices, as I had thought, but the total number of elements in the array. So, for a Transform component, we're talking nine elements per index, right? While I think I understand why it doesn't work, I still can't figure out how to fix it.
How would you refactor the method, given the above constraints?
Answer by jgodfrey · Jun 25, 2016 at 03:43 PM
This...
points[i]
is the transform at the i'th position in the points array. As the error says, you can't ask if a "Transform" is less than some int value (points.Length). You want to compare "i" itself, not points[i]. So...
while (i < points.Length);
Wow... I feel like I need to go back to my first year courses. Thank you for point out my obvious (and embarrassing) error.
Your answer
![](https://koobas.hobune.stream/wayback/20220612083642im_/https://answers.unity.com/themes/thub/images/avi.jpg)