I'm simulating a LiDAR and am working to optimize the code. In following along with the RaycastCommand documentation, I have the core functionality working and I'm trying to optimize it.

I see where the commands and results are being created as NativeArrays with the Temp allocator, so I made them member variables and initialize to the correct size with the Persistent allocator on Start and Dispose them OnApplicationQuit.

However, this still leaves me new-ing thousands and thousands of RaycastCommands every Update. I'd like to initialize the commands once and then just iterate through the NativeArray and change the from and direction properties, which are public according to the documentation, but I'm getting an error that I can't do that when I try.

I'm trying to move the LiDARs through the scene, so I need the origin (from) and direction to change, but I don't want to keep calling all those new commands - is there some way to cache and edit the commands that I'm not noticing? I can't tell why from and direction are public if I'm not allowed to set them.

I've done years of Unity development in Windows with Visual Studio and have recently moved to Linux and VS Code and the auto complete is frankly awful by comparison. It's making it hard for me to look through the autocomplete options for possible alternatives.

  • 1
    \$\begingroup\$ Can you share the full error message you get when trying to assign to from and direction? new is pretty cheap for struct types though, since it doesn't require any GC allocations. Are you sure that's where your bottleneck is in this case? \$\endgroup\$
    – DMGregory
    Sep 9, 2021 at 1:26
  • \$\begingroup\$ @DMGregory - I don't have the exact error message at the moment; it'll be tomorrow morning before I'm back to my computer. I setup a profiler section to monitor that code and creating the RaycastCommands takes about 8.5 ms where the raycast itself (creating the job handle and waiting for completion) only takes about 4.5 ms. I would have thought the raycasting would take far more time than the command creation but that's not the case. \$\endgroup\$
    – Chuck
    Sep 9, 2021 at 2:10

1 Answer 1


The code I was trying to use looked like:

NativeArray<RaycastCommand> commands;
<code to initialize commands>

<loop through the commands>
    commands[currentIndex].from = transform.position;
    commands[currentIndex].direction = dir;

And the error was:

error CS1612: Cannot modify the return value of 'NativeArray.this[int]' because it is not a variable

As described here, (modifying it to be specific to my case)

This is because [the raycast command] is a value type (struct).

Because of this, when you access the [from or direction] property you're accessing a copy of the value held by the class, not the value itself as you would with a reference type (class), so if you set the [from] property on it then you're setting the property on the copy and then discarding it, leaving the original value unchanged. This probably isn't what you intended, which is why the compiler is warning you about it.

I defined a temp variable outside the loop and use that to hold the current command while I modify the parameters, then tuck that back into the NativeArray:

RaycastCommand temp;
    temp = commands[currentIndex];
    temp.from = transform.position;
    temp.direction = dir;
    commands[currentIndex] = temp;

.... and, as @DMGregory predicted, it didn't make any difference at all.

I had included the step where I'm doing the rotation math with the command setup Profiler section, and it seems like the rotation math is actually what's bogging down this process.

  • 1
    \$\begingroup\$ Please ensure you include the error message in your original question in future — we could have solved this immediately with that info, as it's a very common C# error that is not unique to Unity or RaycastCommands. In fact, just typing that error into a search engine would have led you to existing StackOverflow answers explaining how to solve it, so you wouldn't have needed to post a question and wait for a response at all. \$\endgroup\$
    – DMGregory
    Sep 9, 2021 at 16:23
  • \$\begingroup\$ @DMGregory Yeah I understand. \$\endgroup\$
    – Chuck
    Sep 9, 2021 at 16:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .