- Home /
TextMeshPro change value with slider
I have this code from a video (https://www.youtube.com/watch?v=b3S5a_ohZZ0), but it only works with regular text. What it's supposed to do is change the value of the text depending on a slider. How do I get this to work on TextMeshPro? Please watch the video for more details (it's only 2 minutes).
The code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ShowValue : MonoBehaviour
{
Text percentageText;
void Start()
{
percentageText = GetComponent<Text>();
}
public void textUpdate(float value)
{
percentageText.text = Mathf.RoundToInt(value * 100) + "%";
}
}
Answer by DevManuel · Mar 17, 2021 at 08:11 AM
What's the problem?I mean everything is explained in the unity docs. But I will help you:
To get the slider value, place a slider into your canvas and connect it to the script:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Example : MonoBehaviour
{
public Slider mainSlider;
int value;
// I would set this function to the onValueChanged event of the slider.
public void updateValue()
{
Debug.Log(mainSlider.value);
value = mainSlider.value;
}
}
more information can be found here
The next part is setting the text value of a textMeshPro component: Again you can always have a closer look to the docs: There are to types... I think you are using the type for the ui --> TextMeshProUGUI So here the code:
using TMPro;
// ...
public gameObject textObject;
textObj.GetComponent<TextMeshProUGUI>().text = value;
Your answer
Follow this Question
Related Questions
bolt errors 1 Answer
If Statements with PlayerPrefs 1 Answer
I can't change the text mesh 1 Answer
Mesh with Transparent material makes TextMesh in front of it invisible 3 Answers
TextMesh Pro Page content 2 Answers