Hey all. I’m just getting started with Unity, and I’m going through the Roll a Ball tutorial.
I have a question about ToString(). In the tutorial, we’re instructed to write the following:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerController : MonoBehaviour
{
private Rigidbody rb;
public float speed;
private int count;
public Text countText;
void Start()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText();
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal , 0.0f , moveVertical);
rb.AddForce(movement*speed);
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.CompareTag("Pick up"))
{
other.gameObject.SetActive(false);
count = count+1;
SetCountText();
}
}
void SetCountText()
{
countText.text = "Count: " + count.ToString();
}
}
Can anyone explain why “ToString()” is necessary in the SetCountText() function? As far as I can tell, “count” does the job by itself. Is there something I’m missing? Thanks in advance.
To answer your question, it is not necessary. Using that operator with a string and an int is the same thing as calling ToString on the int and using the + operator between the original string and the stringified int.
As far as what I think the implied question is (“why would someone say to do this?”): Some people think that “verbose” and “explicit” are synonyms. The same people tend to advise people to do things like use type names for local variables rather than var. A long time ago, I had a boss who wanted a comment before every line. These are all symptoms of the same misconception.
From an androgogical perspective, I suppose there might be an opportunity to use the explicit ToString call to start a conversation about what ToString is and what it does but a Unity tutorial seems like a less-than-optimal place to start that conversation.
Thanks for shedding some light. I did wonder if it had something to do with getting you in the habit of using ToString() as good practice. I’m at the stage where I know how to get from A to B when writing simple code, but I’m rarely certain that I’m doing it efficiently.
There are many contexts in which it is a good practice but I cannot think of a reason why using the parameterless overload (the one that overrides Object.ToString()) is a good practice. Maybe the person who wrote the tutorial is a JavaScript developer and the weirdness that JavaScript has around the “+” operator is something they are accustomed to working around…or maybe they want this tutorial to work for UnityScript and C#. I don’t know enough about UnityScript to know if it carries over JavaScript peculiarities like that.
It’s not a big deal to merge an int and string.
However, it’s not a bad habit to use ToString() either, because if you try to write:
textObj.text = myInt; // doesn't work without ToString();
I remember being curious about this, and just googled to see if I could find any new update…
Some boxing/unboxing might be occuring, as a tiny performance hit (This is what I tried to google to find out if that changed).
I’m sure if you open up string’s operator +(string, object) with a decompiler, you will see something like this:
public static string operator + (string left, object right) => left + right.ToString();
I’d be skeptical of any performance cost. Although you are right that the way it is done forces the creation of a second string object prior to concatenation…that is, unless there is an high-level optimizer.
Since StringBuilder has an append method that takes an int32 so there is at least a chance to optimize a string of “+” operators into a series of calls to Append on a StringBuilder with a generous initial buffer. Then again, if you’re going to go that far, why not just go one more level and unwrap all the well-known ToString calls.
Anyway. At this point, we’re just measuring who can fart further. I doubt anyone would ever be able to really measure the difference unless they had some incredibly taxing big data problem that involved a lot of string concatenations…far from the domain of this forum.