Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

If Statement - Check if two Text Class have the same text

Discussion in 'Scripting' started by RDG_Admin, Aug 13, 2019.

  1. RDG_Admin

    RDG_Admin

    Joined:
    Oct 4, 2014
    Posts:
    22
    Still learning here...

    I've been able to develop a script I'm working on but I've hit a bit of a snag. I have two public classes:

    Code (CSharp):
    1.  
    2.     public Text CurrentText = null;
    3.     public Text LatestText = null;
    4.     void Start()
    5.     {
    6.         CurrentText.text = System.IO.File.ReadAllText(@"C:\CurrentVersion.txt");
    7.         LatestText.text = System.IO.File.ReadAllText(@"C:\LatestVersion.txt");
    8.     }
    9.  
    I'm trying to create an if statement that compares these two text files to see if the information in both files is identical or not. However using this:

    Code (CSharp):
    1. if (CurrentText == LatestText)
    while this doesn't produce any issues in the Console, from the reading I've done, because using text - this current if statement would only work for numerical values.

    How would I write the if statement to compare two texts?
     
  2. DryerLint

    DryerLint

    Joined:
    Feb 7, 2016
    Posts:
    68
    Perhaps it is String.CompareTo() you seek.

    if (CurrentText.text.CompareTo(LatestText.text) == 0)


    That statement will return true if the two strings are the same.

    Edit:

    Apparently it's bad form to use CompareTo(), although I think it works. You should actually use String.Equals():

    if (CurrentText.text.Equals(LatestText.text))
     
  3. RDG_Admin

    RDG_Admin

    Joined:
    Oct 4, 2014
    Posts:
    22
    Damn, that was a fast response - thanks!

    So I replaced the if statement I had with the one you suggested. To tell if the statement would work - I'd have one of the texts change color... aaaand.... no - didn't work :(

    Posted the full script here, it's probably something I've done...

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using System.IO;
    6.  
    7. public class scriptVersionText : MonoBehaviour
    8.  
    9. {
    10.     public Text CurrentText = null;
    11.     public Text LatestText = null;
    12.  
    13.     void Start()
    14.     {
    15.         Checker();
    16.         CurrentText.text = System.IO.File.ReadAllText(@"C:\CurrentVersion.txt");
    17.         LatestText.text = System.IO.File.ReadAllText(@"C:\LatestVersion.txt");
    18.     }
    19.  
    20.     void Checker()
    21.     {
    22.  
    23.         if (CurrentText.text.CompareTo(LatestText.text) == 0)
    24.         {
    25.             //Green
    26.             LatestText.color = new Color32(76, 212, 105, 255);
    27.         }
    28.        
    29.     }
    30.  
    31. }
    32.  
     
  4. RDG_Admin

    RDG_Admin

    Joined:
    Oct 4, 2014
    Posts:
    22
    oh! just seen your edit - will try it out :)
     
  5. DryerLint

    DryerLint

    Joined:
    Feb 7, 2016
    Posts:
    68
    In your code, the Checker() function runs before you load the text files. Try the following instead:

    Code (CSharp):
    1.         CurrentText.text = System.IO.File.ReadAllText(@"C:\CurrentVersion.txt");
    2.         LatestText.text = System.IO.File.ReadAllText(@"C:\LatestVersion.txt");
    3.         Checker();
    Good luck!
     
  6. RDG_Admin

    RDG_Admin

    Joined:
    Oct 4, 2014
    Posts:
    22
  7. DryerLint

    DryerLint

    Joined:
    Feb 7, 2016
    Posts:
    68
    Haha, no problem!
     
  8. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,745
    Equals returns boolean value, you don't need to compare it with 0, it can be just
    Code (CSharp):
    1.         if (CurrentText.text.Equals(LatestText.text))
    2.