I'm trying to save the angle values where I have two line segments intersecting. I made AngleOne and AngleTwo global variables so that I can refer to them in my SaveElements() method.
When the custom event onDrawingSecondLine is thrown, it checks if the line segments are intersecting and if so, set the angles.
Then I am looping through each line in my lines list and assigning the angle values to be saved in the db.
Here's what I have so far:
string angleOne = "";
string angleTwo = "";
public void onDrawingSecondLine (object sender, LineDrawingEvent e) {
if (LinesIntersect) {
angleOne = CameraObject.GetComponent<DrawLines>().GetAngle();
angleTwo = (180.00 - Double.Parse(angleOne)).ToString();
intersectingLineOne.lineParams.displayName = " " + angleTwo + "(" + angleOne + ")";
string angle = intersectingLineOne.lineParams.displayName;
string col = Color.cyan.ToString();
SaveLinesWithAngles(e.lineName, angle, col);
}
else
{
intersectingLineOne.lineParams.displayName = " ";
angleOne = "";
angleTwo = "";
string angle = intersectingLineOne.lineParams.displayName;
string col = Color.cyan.ToString();
SaveLinesWithAngles(e.lineName, angle, col);
}
}
public void SaveLinesWithAngles(string lineName, string angle, string col)
{
PlanningParams p;
List<PlanningParams> planningParamsList = new List<PlanningParams>();
int lineNumber = 1;
p = SaveElements();
if (p != null)
{
planningParamsList.Add(p);
}
string type = GetLineType(lineName);
string s = XmlSerializeUtility.ToXml(planningParamsList);
lineNumber = currentIntersectingLine;
Application.ExternalCall("BrowserSide.Planning.SaveToSession", s, ViewType, angle, "LinesWithAngles", col, lineNumber);
}
private PlanningParams SaveElements() {
PlanningParams planningParams = null;
planningParams = new PlanningParams();
foreach (Line line in CameraObject.GetComponent<DrawLines>().GetLinesWithAngles())
{
LinesWithAnglesData linesWithAnglesData = new LinesWithAnglesData();
linesWithAnglesData.angleValue1 = float.Parse(angleOne);
linesWithANglesData.angleValue2 = float.Parse(angleTwo);
planningParams.LinesWithAnglesData.Add(linesWithAngleData);
}
}
public List<Line> GetLinesWithAngles()
{
List<Line> linesWithAngles = new List<Line>();
int i = 0;
if (lines == null)
{
return null;
}
for (i = 0; i < lines.Count; i++)
{
if (lines[i].elementType == ElementType.Line && lines[i].lineParams.lineName.Contains("IntersectingLine"))
{
linesWithAngles.Add(lines[i]);
}
}
return linesWithAngles;
}
Why are the values of Angle One and Angle Two always updated to the new value and don't keep their current values? How can I make it so the values are not updated to new value?
EDIT:
Seems like a similar issue like this guy had: Why does adding a new value to list<> overwrite previous values in the list<>
But I do have my instance inside my foreach loop. Inside the foreach loop, I noticed that the old value is overriden with the new value.