- Home /
How to campare gameObject.name with string when gameObject have additional numbers or words?
public GameObject myObject;
public string myObjectStringName;
if(myObject.name == myObjectStringName)
{
}
The problem is in additional words, like object count numbers or information about clone. For example myObject(111) or myObject(clone) is not equal "myObject" string cause contains additional words.
You'll want to research the documentation on C# string comparison functions. The details are beyond the scope of a post (though I'm known to go on and on :)...
Basically, you're either looking for String.StartsWith or String.IndexOf, both of which match substrings. StartWith would find myObject in myObject(111) or myObject(clone). IndexOf would find myObject inside anothermyObject.
You'll also find case insensitive matching, as well as linguistic (sound alike) and a wide range of other string matching features to suit just about any need along these lines.
Answer by Vedang202 · Jul 10, 2018 at 12:41 PM
Use this
public GameObject myObject;
public string myObjectString;
string tempMyObjectString;
tempMyObjectString = myObject.name.Substring (0,8);
if(tempMyObjectString == myObjectString)
{
}
by doing so you will be shrinking the "myObject" name and removing the data after 8 characters and thus if both Objects have same 8 characters the if statement will be true!
Answer by Zodiarc · Jul 10, 2018 at 12:09 PM
Either what JVene said in his comment or you dig into Regular expressions
Your answer
![](https://koobas.hobune.stream/wayback/20220612170423im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Photon Unity : rename an instatiate GO and synchronize the name 1 Answer
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Check if GameObjects are from the same prefab? 1 Answer
Find specific prefab in a list 2 Answers
How to obtain a gameObject from an List with the name? 1 Answer