How can I check if the text string is not equal and then to do something?

using UnityEngine;

public class DrawLines : MonoBehaviour
{
public LineRenderer lineRenderer;
public string text;

private Vector3[] positions;
private string oldText;

private void Start()
{
if (lineRenderer == null)
{
lineRenderer = GetComponent<LineRenderer>();

lineRenderer.startWidth = 0.3f;
lineRenderer.endWidth = 0.3f;
}

// 0,  0, 0
// 5,  0, 0
// 5, -5, 0
// 0, -5, 0

positions = new Vector3[5] { new Vector3(0, 0, 0), new Vector3(5, 0, 0),
new Vector3(5, -5, 0), new Vector3(0, -5, 0), new Vector3(0, 0, 0)};
DrawLine(positions, Color.red, 0.2f);
}

void DrawLine(Vector3[] positions, Color color, float duration = 0.2f)
{
GameObject myLine = new GameObject();

myLine.transform.position = positions[0];
LineRenderer lr = myLine.GetComponent<LineRenderer>();
lr.positionCount = positions.Length;
lr.startColor = color;
lr.endColor = color;
lr.startWidth = 0.1f;
lr.endWidth = 0.1f;
lr.useWorldSpace = false;
lr.SetPositions(positions);
}

void DrawText()
{
for (int i = 0; i < positions.Length; i++)
{
if (oldText != text)
{
var pos = Camera.main.WorldToScreenPoint(positions[i]);
text = positions[i].ToString();
var textSize = GUI.skin.label.CalcSize(new GUIContent(text));
GUI.contentColor = Color.red;
GUI.Label(new Rect(pos.x, Screen.height - pos.y, textSize.x, textSize.y), text);
GUI.contentColor = Color.green;
GUI.Label(new Rect(pos.x, Screen.height - pos.y - 20, textSize.x, textSize.y), i.ToString());
}
}
}

private void OnGUI()
{
if (positions != null)
{
if (positions.Length > 0)
{
DrawText();
}
}
}
}


I added oldText variable but not sure how use it more then I did so far. I want that if the text is the same in the loop then don't show this text not in red not in green. Each time show the text once if it's not equal.

When the square is closed :

• if (oldText != text) is how you would check if they are not equal. Other than that, I don't understand what you are asking. – Kevin Jul 31 at 23:50
• @Kevin I know how to make this comparison but oldText will be empty all the time. Where do I make oldText to be equal to the text ? I need some where to update the oldText. like oldText = text; but not sure where. – Benzi Avrumi Jul 31 at 23:56
• What is the feature you're trying to achieve? – Vaillancourt Aug 1 at 0:34
• @Vaillancourt I'm using linerenderer to draw lines and in each lines corner where two lines meet I write with a text and label gui the position coordinate. I added now a screenshot to my question. It's working fine with the text but there is a case/s that the last point meeting position is the same like the first for example if I will close the square back to the position 0,0,0 then it will show the same text and position on the one that already exist. So for example in this case of the first and end positions I want it to show the text only once. – Benzi Avrumi Aug 1 at 0:47
• @Vaillancourt there is no need to show twice the position. That is why I want to check that if it's the same text/position don't show the text again. – Benzi Avrumi Aug 1 at 0:48