Search Unity

How to modify return value in struct

Discussion in 'Scripting' started by Rewaken, Jan 21, 2019.

  1. Rewaken

    Rewaken

    Joined:
    Mar 24, 2015
    Posts:
    128
    I am getting the following error.

    Can not Modify return value because it is not variable

    I am using a mutable struct for making my own custom native container to use unity job system and I can't use the reference type in it. I understand that it is happening because of structs and value types but I can't use any reference type. Following is my code. I changed code bit by adding method but still not getting correct output is this is the correct way to change values in the struct?

    Code (CSharp):
    1. public struct TObj
    2. {
    3.     // CustomArray is NativeContainer
    4.     public CustomArray<TZone> array;
    5.  
    6. }
    7.  
    8. public struct TZone
    9. {
    10.     public CustomArray<Vector3> vectors;
    11.     public void SetValue( int t, Vector3 c)
    12.   {
    13.      this.vectors[t] = c;
    14.     MonoBehaviour.print(c);
    15.   }
    16. }
    17.  
    18. struct ColSetter: IJobParallelFor
    19. {
    20. public CustomArray<TObj> Objs;
    21. public NativeArray<Vector3> pos;
    22.  
    23. public void Execute(int i)
    24. {
    25.     //Objs[i].array[i].vectors[i] =  pos[i];//Error
    26.         //Change Line To
    27.         Objs[Ids[i]].array[MetaIds[i]].SetValue(MetaIds[i], pos[i]);
    28.         MonoBehaviour.print(Objs[Ids[i]].array[MetaIds[i]].vectors[i] + " Vectors" );
    29. }
    30. }
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,770
    Not sure I is going to help, but try first assign position to vector array, then assign its result to your array, as in separate line. And similar for objects array assignment.
    Basically make it in smaller steps.