Home > Blockchain >  Unity - Copy List<Vector3> from sriptable object to another at certain point of runtime
Unity - Copy List<Vector3> from sriptable object to another at certain point of runtime

Time:03-17

I'm making a time trial race game, so when the player finishes a lap a ghost will copy the best lap the player did.

I use two scriptable object to store the data of the best lap and the actual lap.

My problem is when I delete the data of the actual lap the data of the best lap is also deleted.

In the GameManager if the player beats his own time then I do:

GhostController.sharedInstance.bestLap.carPositions = GhostController.sharedInstance.actualLap.carPositions; GhostController.sharedInstance.bestLap.carRotations = GhostController.sharedInstance.actual.carRotations;

And to clear the list of the actual data :

GhostController.sharedInstance.actualLap.carPositions.Reset(); 
GhostController.sharedInstance.actual.carRotations.Reset();

I have to use the singleton pattern to access to the GhostController methods so in the GhostController class I have:

public GhostLapController actualLap; 
public GhostLapController bestLap; 

public static GhostController sharedInstance;

public GhostLapController actualLap;
public GhostLapController bestLap;

private void Awake()
{
    if(sharedInstance == null)
    {
        sharedInstance = this;
    }
}

And in the ScriptableObjectController I have:

 [CreateAssetMenu]
 public class GhostLapController : ScriptableObject
{
public List<Vector3> carPositions;
public List<Quaternion> carRotations;

public void AddNewData(Transform transform)
{
    carPositions.Add(transform.position);
    carRotations.Add(transform.rotation);
}

public void GetDataAtPosition(int sample, out Vector3 position, out Quaternion rotation)
{
    position = carPositions[sample];
    rotation = carRotations[sample];
}

public void Reset()
{
    carPositions.Clear();
    carRotations.Clear();
}

}

It's like this asignation it's always reading the actualLap data instead of getting a screenshot of it's value at execution time.

GhostController.sharedInstance.bestLap.carPositions = GhostController.sharedInstance.actualLap.carPositions; GhostController.sharedInstance.bestLap.carRotations = GhostController.sharedInstance.actual.carRotations;

Could you help me please?

CodePudding user response:

In

GhostController.sharedInstance.bestLap.carPositions =  GhostController.sharedInstance.actualLap.carPositions;
GhostController.sharedInstance.bestLap.carRotations = GhostController.sharedInstance.actual.carRotations;

you do exactly what you describe!

after this e.g. the

GhostController.sharedInstance.bestLap.carPositions

and

GhostController.sharedInstance.actualLap.carPositions

will both hold references to the exact same collection instance!

If you rather want a copy then do e.g.

GhostController.sharedInstance.bestLap.carPositions = new List<Vector3>(GhostController.sharedInstance.actualLap.carPositions);
GhostController.sharedInstance.bestLap.carRotations = new List<Quaternion>(GhostController.sharedInstance.actual.carRotations);
  • Related