How to use Trailrenderer.Getpositions?

Hello! I want to use Trailrenderer.Getpositions function.
But I’m not good at Unity and C# so really confused about using that function.

How can I use Trailrenderer.Getpositions properly?

Or is there any function that can find trails vertex position?


[TrailRenderer.GetPositions][1] returns an int and has an out parameter of type Vector3.

The int is just how many vertices you have.

The Vector3 is the array of vertices themselves, and it what you want.

If you haven’t used the out keyword before, it’s a write-only version of ref, and is essentially used as a second return statement. Physics.Raycast uses out as well to return data about the hit point. You may have also run across it’s cousin ref. Unlike ref, however, out is write-only and does not require that the object it references is initialized. It is guaranteed to be set after the function call if no exception occurs, just like a return statement. You use it like this:

TrailRenderer render = GetComponent<TrailRenderer>(); //Or however you want to do it.

Vector3[] positions; //No need to initialize the array. Out does this for us.
render.GetPositions(positions); //This function returns an int, but we don't really need it, since we have positions.Length.

//positions now contains the vertices, and you can do stuff like:

for(int i=0; i<positions.length; i++){

[Here][2] is a good link about ref and out.
[The Ref Keyword][3] is used to pass in an argument by reference, and can both read and write to it.
[The Out Keyword][4] is like Ref, but write-only. The variable passed into it must be written to, and need not be initialized. A function that uses Out can be used to initialize a variable.
It’s common to see people using Ref and Out to create functions that return many values at once, however for a number of reasons, [including performance][5], this can be a bad idea. Returning more data can be achieved via Tuples or wrapping things in Structs. That’s not to sat that Ref or Out are evil by any means, they have their uses, but they are also often misused.
[1]: Unity - Scripting API: TrailRenderer.GetPositions
[2]: ref vs out in C#
[3]: ref keyword - C# Reference | Microsoft Learn
[4]: out parameter modifier - C# Reference | Microsoft Learn

Actually the documentation is misleading / wrong. The positions parameter is not an “out” parameter. It’s actually a normal parameter and the method expects you to initialize the array yourself. The method will fill in the positions into the array and returns how many elements has been written. This pattern is used in order to reuse the array so you don’t have to allocate a new array every time you call the method.

So it works like this:

Vector3[] positions = new Vector3[200]; // allocate large enough

// inside a method:
int count = trailRenderer.GetPositions(positions);
for(int i = 0; i < count; i++)
    Vector3 p = positions*;*

Note the use of “count”. You can not simply iterate through the whole array since only count elements has been written.

Thanks for replying!
But, I ended up using a particle system instead for the “trail points” I wanted.