Home > Blockchain >  When TEXT is SOMETHING then do THAT - Unity 2D
When TEXT is SOMETHING then do THAT - Unity 2D

Time:09-22

I wanted to do the thing, when Text equals "Cherries: 12" then do GameJolt.API.Trophies.Unlock(******);

The problem is, when I do this:

if(Text.Equals = "Cherries: 12")
        {
            GameJolt.API.Trophies.Unlock(******);
        }

It says Cannot assign to 'Equals' because it is a 'method group' Also, I'm new to programming, so if I did anything wrong here, please inform me!

Can someone help me please?

CodePudding user response:

Equals is a function so you should do Text.Equals("Cherries: 12") instead, or Text =="Cherries: 12 "

This could help you to understand better the operators, so you could understand better what they do and how to use them https://www.tutorialspoint.com/csharp/csharp_operators.htm

CodePudding user response:

What type is Text? Is it of type UnityEngine.UI.Text? Or is it of type string? The reason you are getting the error Cannot assign to 'Equals' because it is a 'method group' is because you are calling the method Equals and attempting to assign the value "Cherries: 12" to it.

Instead of using the assignment operator =, you want to use a comparison operator ==. It will compare to values. However, as you are already using Equals, you do not need any sort of comparison, you can simply pass in the value to the method.

If your Text is a UI object, the code should look like:

if(Text.text.Equals("Cherries: 12"))
{
    GameJolt.API.Trophies.Unlock(******);
}

However, if the Text is a string object, the code should look like:

if(Text.Equals("Cherries: 12"))
{
    GameJolt.API.Trophies.Unlock(******);
}

Alternatively, you can use the comparison operator without needing to use the Equals method.

if(Text == "Cherries: 12")
{
    GameJolt.API.Trophies.Unlock(******);
}

CodePudding user response:

Oh yeah, when I changed it to

if(Text.text.Equals("Cherries: 12"))
{
    GameJolt.API.Trophies.Unlock(******);
}

It's working. Thanks!

  • Related