Reversing a List

I’m pretty sure that this is a syntax error but I haven’t been able to find an example. Basically I want to Reverse() a List. The following code (simplified from actually script) produces this error.
‘Reverse’ is not a member of ‘Boo.Lang.List’.

var List1 : List.<GameObject> = new List.<GameObject>();
var List2 : List.<GameObject> = new List.<GameObject>();
 
function Update() {
	List1 = List2.Reverse();
}

Suggestions please.

use System.Collections.Generic.List. so you are on the .NET List<>, not the boo List<>

I think I managed the syntax. Thanks for the information. But now I get a new error.

Cannot convert ‘void’ to ‘System.Collections.Generic.List’.

What am I doing wrong?

var List1 : System.Collections.Generic.List.<GameObject>  = new System.Collections.Generic.List.<GameObject>();
var List2 : System.Collections.Generic.List.<GameObject>  = new System.Collections.Generic.List.<GameObject>();
 
function Update() {
	List1 = List2.Reverse();
}

I should probably mention what I’m hoping to use this for in case my logic is flawed. I have a series of points which lay out a route for a vehicle. I wanted to use a lineRenderer to show the path between points. Unfortunately the lineRenderer pinches at corners so I thought that I could fix this by adding a second lineRenderer that drew in the reverse order. Triangle + triangle inverted = (slightly strange) rectangle. Any flawed logic there?

1 Like

Reverse doesn’t return a list (check the signature here). Which is why the compiler thinks you’re trying to convert nothing (void) to a List. If you want to copy and reverse a list, you’ll need to do the following:

import System.Collections.Generic;

var List1 : List.<GameObject>  = List.<GameObject>();
var List2 : List.<GameObject>  = List.<GameObject>();
 
function Update() {
    var listArray : GameObject[] = new GameObject[List2.Count];
    List2.CopyTo (listArray);
    List1 = List.<GameObject> (listArray);
	List1.Reverse();
}

That said, what I’ve provided is probably the simplest approach to do that, but by no means the fastest. Also, why would you do something that causes a non-changing result constantly in Update (rather than in, say, Start or Awake)?

Thanks for the info burnumd. The script above is a simplified version of the script I’m working on to fix a lineRenderer pinching problem. I just wanted to make things easy to lay out so that I could communicate the problem. The actual script will only do the reversing after certain conditions are met.

I was able to implement your code but unfortunately the solution I devised does not work.

I think I’ll start a new thread as this one has technically been solved.

Thanks again.