- Home /
How to compare names of two GameObjects to check whether their names are equal or not?
I am making a puzzle game. i have a sprite named A1 with a box collider2D,rigidbody2d component and an empty gameobject with just a box collider2d and its also named as A1. what i want is when i drag my sprite over the empty gameobject, its position becomes equal to that of the empty object.but if the names of the two gameobjects are same i want it to get locked in that position. to do so , in OnTriggerStay2D() method i am writing a code i attached: but the string.compare() method is returning a value -1.but the names are same it should return 0. i dont know where i am wrong. please help.
Answer by ojelibalon · Sep 07, 2018 at 03:40 PM
It should actually work. Have you tried printing the names (Debug.Log) and see if they really are the same? Or you could use tags instead of comparing the names
Yes I tried printing their name.. As u can see in the code "debug. Log(this.name)..." Names are same I think string. Compare(string, string ) doesn't work for numeric values in name. Because I changed the name of object from A1 to A and the code worked. But I want to know what is the exact concept behind it.
If anyone knows please help
Your answer
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
How to campare gameObject.name with string when gameObject have additional numbers or words? 2 Answers
How to obtain a gameObject from an List with the name? 1 Answer
All subsequent instantiated objects = auto-named with suffix (Clone)? 1 Answer