I am new to Unity, and I am trying to do something which includes changing the values of a TextMeshPro object. I used this code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class Main : MonoBehaviour
{
public GameObject[] myObjects;
public TextMeshProUGUI textCount;
string ObjectCount;
// Start is called before the first frame update
void Start()
{
textCount = GameObject.Find("/Canvas/ObjCount").GetComponent<TextMeshProUGUI>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
int randomIndex = Random.Range(0, myObjects.Length);
Vector3 randomSpawnPosition = new Vector3(Random.Range(-10, 11), 5, Random.Range(-10, 11));
Instantiate(myObjects[randomIndex], randomSpawnPosition, Quaternion.identity);
ObjectCount += 1;
textCount.text = ObjectCount;
}
}
}
However, instead of 1+1 being 2, 1+1, to the script, is apparently 11. Press space again, it’s 111, and so on. Is there any way to fix this?