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.124146-forum.png

2 Answers

2

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

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