- Home /
Other
error CS0019: Operator `==' cannot be applied to operands of type `UnityEngine.GameObject' and `string'
Hello, I am running into a wall here and I would like help. I have this empty parent game object that holds many children cube game objects that has colliders and another empty game object that has a collider of its own.
I want each of the respective children game object have their things called in the OnTriggerEnter because each children game object is supposed to cause an event in the game that I want it happen such as a dialogue box appearing.
However, it says I can't have strings to compare if two gameobjects have collided with eachother and I would like people's help if they could give me advise. Thanks in advance, I will continue to keep fiddling with this.
Here is the code I'm trying to work with:
public class Wave0TutorialEventManager : MonoBehaviour
{
public string collisionTagPlane = "PlayerPlane"; //The Player Plane Game Object
private GameObject cameraGUI; //The camera that will show all the text Boxes
private GameObject wave0TutorialEvent1;
private GameObject wave0TutorialEvent2;
void Start()
{
cameraGUI = GameObject.FindGameObjectWithTag ("TextBoxCamera");
wave0TutorialEvent1 = GameObject.Find ("Wave0TutorialEvent1");
wave0TutorialEvent2 = GameObject.Find ("Wave0TutorialEvent2");
}
void OnTriggerEnter(Collider col)
{
if(wave0TutorialEvent1 == collisionTagPlane)
{
cameraGUI.SendMessage ("Wave0TutorialEvent1", true);
Debug.Log ("Player Area has reached this point");
}
if(wave0TutorialEvent2 == collisionTagPlane)
{
cameraGUI.SendMessage ("Wave0TutorialEvent2", true);
Debug.Log ("Player Area has reached second point");
}
}
}
Answer by Anxo · Jan 26, 2015 at 06:49 PM
Replace
if(wave0TutorialEvent1 == collisionTagPlane)
With
if(wave0TutorialEvent1.name == collisionTagPlane)
b/c what you are doing is (GameObject is equal to String?) but what you want to do is (StringA is equal to StringB?)
Answer by meat5000 · Jan 26, 2015 at 06:06 PM
wave0TutorialEvent1.name
I'm assu$$anonymous$$g that should be placed in the comparison part of the if statements. I'll go try that now.
Indeed. It is how to access the actual String name of the GameObject, as seen in the inspector.
Note that String comparison is slow, but you won't have to worry about this most of the time.