- Home /
Why does this text not equal the game object's name?
I am trying to set this if statement to true if the text in a textbox is equal to the game objects name.
Debug.Log(nameText.text);
Debug.Log(gameObject.name);
if (nameText.text == gameObject.name)
{
Debug.Log("True");
}
else
{
Debug.Log("False");
}
The output is:
NPC
NPC
False
$$anonymous$$ight sound stupid, but try
if (string.Compare(nameText.text, gameObject.name, false) == 0)
{
Debug.Log('true");
}
else
{
Debug.Log('false");
}
Your nameText might have a new line at the end of it maybe?
Answer by Eugene_boomzap · Feb 13, 2019 at 05:30 AM
I tested something similar and it worked perfectly.
using UnityEngine;
using UnityEngine.UI;
public class TestText : MonoBehaviour
{
public Text nameText;
void Start()
{
Debug.Log(nameText.text);
Debug.Log(gameObject.name);
Debug.Log(nameText.text == gameObject.name);
if (nameText.text == gameObject.name) {
Debug.Log("True");
}
else {
Debug.Log("False");
}
}
}
And the Console result: I suggest you double check whether the two texts are equal or not. For instance, there might be a space or change line mark at the end.
If it does not solve your issue, you may paste more code and I will check for you.
Best Regards,
Your answer
Follow this Question
Related Questions
GetComponent, int error, if statement, problem. 1 Answer
How to use multiple If statements? Or can I use relational operators in switch statements? 1 Answer
Add text on top of a gameobject. What is the best way? 1 Answer
Instantiate a GameObject at a position specific to an element that was found in a text 1 Answer
Dynamically assign each ui.Text.text in list to the name of objects in another list 2 Answers