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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Check is a string contains text

Discussion in 'Scripting' started by Klaes4Zaugen, Apr 29, 2015.

  1. Klaes4Zaugen

    Klaes4Zaugen

    Joined:
    Apr 24, 2015
    Posts:
    5
    I want to convert input from a GUI.TextField to numbers (which I have done) but how do I check if it contains text?

    Current code

    http://pastebin.com/jQZLDFkt (don't know how to format code on Unity forums)

    I need a if check to see if the objective_CTextFieldString contains text like 53x = false, 53 = true, x = false.
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,998
  3. Klaes4Zaugen

    Klaes4Zaugen

    Joined:
    Apr 24, 2015
    Posts:
    5
    I wanna increase a number in a variable that affects how the game reacts. Well short an experience system. People earn points and then use those points for xp/other stuff. Any recommendations for some other logic, I'm new to Unity.
     
  4. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    yeah. Int.parse. or int.tryparse.

    Google those two things...they allow you to parse a string and see if the value passed in was an int.
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    TryParse will work. However, I've also found it cumbersome and weird, preferring try-catch with int.Parse:
    Code (csharp):
    1.  
    2. bool containsNonNumberText = false;
    3. int numberParsed = -1;
    4. try {
    5. numberParsed = int.Parse(yourText);
    6. }
    7. catch {
    8. containsNonNumberText = true;
    9. }
    10.  
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    That seems a lot more cumbersome than just
    Code (csharp):
    1.  
    2. int num;
    3. if (int.TryParse(myText, out num))
    4. {
    5.     DoNumberThings(num);
    6. }
    7.  
     
  7. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    Yeah, idk what you're doing with that bool in there...tryparse won't return anything if the text isn't a number...so...not sure why you're doing what you're doing.
     
  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    TryParse returns a bool; if it returns true, it found a valid number, if it returns false, it didn't. If it found a number, it will change the value of the second parameter you feed it, to that number.

    I guess cumbersome wasn't really the word, just weird.
     
  9. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Looks odd at first but is pretty standard. Conceptually works the same as Dictionary.TryGetValue and Physics.Raycast
     
    Westland likes this.