Having trouble converting text to string

I get an error that says “Cannot apply indexing with [ ] tp an expression of type ‘method group’”

    public TextMesh mesh;
    public Text ScoreText;
    int CurrentScore= 0;
    float[] Value;

    void Start () {
        ScoreText.text = "Score: " + CurrentScore;
        mesh.text = Value.ToString [Random.Range (20, 100)];
    }

    void Update () {
   
    }
}

Value is a float array. If you remove the square brackets the error will go away, but what is the purpose of this float array? And is there a reason why it is an array?

Another possibility is that you put the operations backwards, should be this:

mesh.text = Value[Random.Range(20, 100)].ToString();

But Random.Range might give a float, which isnt usable as an index in an array, so it has to be converted into an int. And even if it did work, it makes me wonder why there are 100 values, and why the first 20 are excluded.

1 Like

also; ToString is a function, needs the ()

If you give it ints it’ll give you back an int - so it would work in this particular case.

to that point, there are two version of Random.Range() one takes two ints, one takes two floats.

The int version is exclusive i.e. Random.Range(0, 1) will yield only 0s
The float version is inclusive i.e. Random.Range(0f, 1f) might yield 1.0

Thank You

I am well aware of the overload method, but I’ve had situations where the int overload didn’t come up when I just put in numbers like that, and I would need to deliberately Ceil or Floor the value.

i have 3 new errors

“NullReferenceException: Object reference not set to an instance of an object
ScoreScript.Start () (at Assets/Scripts/ScoreScript.cs:14)”

NullReferenceException: Object reference not set to an instance of an object
ScoreScript.Start () (at Assets/Scripts/ScoreScript.cs:15)

NullReferenceException: Object reference not set to an instance of an object
ScoreScript.Start () (at Assets/Scripts/ScoreScript.cs:14)

Can’t say I’ve ever had it go that way - I have experienced scenarios where I wanted a float and got an int back by mistake though. Sort of the downside of those implicit downcasts.

The excerpt in the OP doesn’t line up with those line numbers, so no idea. Post the whole script with the numbering aligned.

From the type of message - are all your object references assigned? ScoreText, mesh, and any others?