Random unused variable in function completely changes script output like a ghost

Im working on a line chopping function, in certain cases I want to call this function because i sometimes need to slightly extend a line so that it will register with the intersection of other lines and my script will work. The below does nothing, im still working on it but when i mess with either newLine or even bugLine it messes with my whole game!

static public LineSegment ExtendLine(LineSegment line, float dist)
    {
       
        LineSegment newLine = line;
       
        LineSegment bugLine = newLine;

        // Debug.DrawLine(bugLine.points[0] - Vector3.up, bugLine.points[1] - Vector3.up, Color.green, 20000, true);

        Vector3 bb = bugLine.points[0] - bugLine.points[1];
        bb = bb.normalized;
        float mag = bugLine.points[0].magnitude - bugLine.points[1].magnitude;
        Vector3 first, second;
        first = bugLine.points[0] + (bb * 0.1f);
        second = bugLine.points[1] - ( bb * 0.1f);
        //Debug.DrawLine(first - Vector3.up, second - Vector3.up, Color.green, 20000, true);
        //bugLine.points[0] = first;
        //bugLine.points[1] = second;
        return newLine;
    }

the above doesn’t change anything but it also doesnt return an extended line

[CODE]
static public LineSegment ExtendLine(LineSegment line, float dist)
    {
       
        LineSegment newLine = line;
       
        LineSegment bugLine = newLine;

        // Debug.DrawLine(bugLine.points[0] - Vector3.up, bugLine.points[1] - Vector3.up, Color.green, 20000, true);

        Vector3 bb = bugLine.points[0] - bugLine.points[1];
        bb = bb.normalized;
        float mag = bugLine.points[0].magnitude - bugLine.points[1].magnitude;
        Vector3 first, second;
        first = bugLine.points[0] + (bb * 0.1f);
        second = bugLine.points[1] - ( bb * 0.1f);
        //Debug.DrawLine(first - Vector3.up, second - Vector3.up, Color.green, 20000, true);
        bugLine.points[0] = first;
        bugLine.points[1] = second;
        return newLine;
    }

the above messes with the stuff outside of the function, which makes no sense!!!
any ideas?? thanks

Not sure what you mean. What is the issue? I see you’re adding a value to your bugLine points and then setting the values in the second script, but not sure what you mean by “messes with stuff outside the function”

sorry. So in both situations the function returns exactly what it is given via input, however when i take the comments out on the lines that set the 2 points in bugLine (which is not returned or relevant inside or outside of the function) it messes up the lines outside of the function, creates more bugs etc.
it literally makes no sense and seems impossible

What is ‘LineSegment’?

It appears to have an array.

You appear to be changing the array in the 2 lines they you uncomment in the ‘bugged’ version.

Just because you set ‘bugLine = newLine’, doesn’t necessarily mean you have a copy. If LineSegment is a class, you have a reference to the same object, so modifying one modifies both. If LineSegment is a struct, well the array in question is still a class, and therefore reference type. So you end up having a copy of LineSegment, but said copy references the SAME array.

So you’re essentially modifying line no matter what.

If you intended to modify those arrays, and the bug is something other than the points… well, then we need more information. Being told “it messes up the lines outside of the function” is far to vague.

1 Like