Losing variable reference after run or scene reload

Hi there!

Let’s say I have this structure:

public class Spline
{
     public List<BezierPoint> points = List<BezierPoint>();
     public List<BezierSegment> bezierSegments = List<BezierSegment>();
}

public class BezierSegment
{
    public BezierPoint p0, p1;
}

public class BezierPoint
{
    public Vector3 pos = Vector3.zero;
}

Then I init in the editor (not during the gameplay) the Spline with, let’s say, 3 points, and I define its bezier segments.

s = new Spline()
s.points.Add(new BezierPoint());
s.points.Add(new BezierPoint());
s.points.Add(new BezierPoint());
b0 = bezierSegments.Add(new BezierSegment());
b1 = bezierSegments.Add(new BezierSegment());
b0.p0 = s.points[0]; b0.p1 = s.points[1];
b1.p0 = s.points[1]; b1.p1 = s.points[2];

Every time I move or change any of the three points in the Spline, the points on the Bezier Segments are also updated (they’re the same thing in memory), but as soon as I run the game or reload the scene, they’re not the same anymore and moving the Spline points won’t make the Bezier Segment points do the same thing.

I don’t know how to solve this so those points on the Bezier Segments keep connected to the points of the Spline. ANY help will be appreciated.

Thankss!!

Is the BezierPoint a struct? It should be a class in order to keep it in the list as a reference.
And reloading the scene will reset the variables. Have you implemented DontDestroyOnLoad ?

BezierPoint is also a class. I updated the code to show you. I need it to be a class, it has a ton of functions and variables (I’m just showing here the basics)

DontDestroyOnLoad would work to keep the variables after running or reloading, but not after opening the project again I guess.

If you assign values to variables during gameplay, but then close the game, the values are lost. But I’m not sure if that’s what you are asking. Are you talking about keeping values in the Unity editor?

Thanks Brathnann. The variables are assigned in the editor, not during the gameplay. The problem comes after running the game or after reloading the scene. The point 1 in the Bezier Segment and the point 1 in the spline are not anymore one single thing, but separate variables.

It’s not that I loose the content of the variables. What I loose is the fact that point 1 is the same piece of memory for the Bezier Segment and the Spline.

What I try to get is that if I do this:
s.points[1].position = new Vector3(2,1,1);

This updates point1 in the spline, but also point1 in the bezier segments that content the point (segment 0 and segment 1)

Sorry, I’m not clear on what you want versus what you are seeing. Please put some debug.log statements in your code (and their output at runtime) to demonstrate?

How are you saving the splines in editor? I expect the issue is being caused by how the spline is serialized & deserialized. You may want to just rebuild your spline beizer point list from the points in bezier curve or vice versa.