Search Unity

Question How can I check if text content is " "?

Discussion in 'UGUI & TextMesh Pro' started by ReeDimm, Apr 17, 2022.

  1. ReeDimm

    ReeDimm

    Joined:
    Jul 13, 2021
    Posts:
    29
    How can I check if text content is space with "if" statment?
     
  2. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    if (text == " ");

    ...among several million other ways of doing it. Might want to use String.Contains if there's other formatting (such as a line break and so on). Strings contain sequence characters as well, which aren't always visible.
     
    ReeDimm likes this.
  3. ReeDimm

    ReeDimm

    Joined:
    Jul 13, 2021
    Posts:
    29
    Still showing error thought...


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6.  
    7. public class Sub : MonoBehaviour
    8. {
    9.     public GameObject DisText;
    10.  
    11.     void Update()
    12.     {
    13.         if (DisText = " ")
    14.         {
    15.          
    16.         }
    17.     }
    18. }
     
  4. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    A GameObject is not a text string but a different class and you can look up what it contains for example the .name property.

    If you wanted only text to manipulate, then define it as:

    Code (CSharp):
    1. public string DisText;
    Which can then be accessed how you expect, for strings.

    But if you mean TextMeshPro then you should say so, as that will be an object itself, and contain it's own .text property.
     
    ReeDimm likes this.
  5. JuliaP_Unity

    JuliaP_Unity

    Unity Technologies

    Joined:
    Mar 26, 2020
    Posts:
    698
    You're missing another equal sign, when you compare equality you have to do
    if (DisText.text == " ")
    (2 times the = character).
     
    hippocoder likes this.
  6. junayedlimon

    junayedlimon

    Joined:
    Oct 17, 2021
    Posts:
    1
    if (Text.contains("Text"))
    {
    Do ...
    }

    Epic Solution . Try it . If your game object name is GameObject01.
    Then No problem . Type Just GameObject
    if (Text.contains("GameObject"))
    {
    Do ...
    }
    Done .