Why parentheses?

Why do we assign a string with parenthesis?

eventText.text = ("hello world");

I see that this works also:

eventText.text = "hello world";
4 Likes

Hi Paolo, welcome to the community :slight_smile:

You don’t, very unnecessary in this case.


See also;

2 Likes

thank you for the answer Rob.

Actually my question is because I would like to know what do parentheses mean around a string. What is the difference in putting them or not?

Thank you for your patience.

1 Like

It doesn’t mean/do anything.

With math you will find times where you need them, but not with strings.

1 Like

thank you very much!

1 Like

You’re very welcome :slight_smile:

1 Like

I personally tend to use parentheses more than I need to with strings because its a habit based upon joining multiple things together… eg. Debug.Log(“string text” + variable). But as Rob says, they are completely not needed in your example - they dont hurt, but aren’t needed.

4 Likes

oh great! now I get it! Thank you Rick.

Thank you! So Debug.Log() due to being a method would need the parenthesis if i understand correctly but I had been using something without parenthesis to concatinate like
scoreText.text = "Score: " + score;
I was wondering if there was a difference but both seemed to work so thank you for confirming :slight_smile:

Yes, Log is a method within Debug, hence the parenthesis, these would be where parameters (if any) are passed in - note this is required for a method, not just because you are concatenating parameters.

3 Likes

Privacy & Terms