Set Text with textmesh pro - c#

Is there any way to cycle text with textmesh script?
I'm interested in cycling text so i can display different text on my 2D game.
I have try the script on this website. But I don't think it works. Maybe I'm doing something wrong?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class CyclingText : MonoBehaviour
{
public TMP_Text text;
void Update()
{
TextMeshPro textmeshPro = GetComponent<TextMeshPro>();
textmeshPro.SetText("The first number is {0} and the 2nd is {1:2} and the 3rd is {3:0}.", 4, 6.345f, 3.5f);
// The text displayed will be:
// The first number is 4 and the 2nd is 6.35 and the 3rd is 4.
}
}
Can someone provide me with an answer or explained what I done wrong?

You seem a bit confused about the use of TMP
1
If you set the TMP in the inspector as public you can simply
public TMP_text text;
And then simply drag the text inside the inspector and simply:
text.text = "Text displayed";
2
If you don't create a public variable but a private, you can:
private TMP_text text;
And then, if you script is in the text object you can:
text = this.gameObject.GetCOmponent<TextMeshPro>();
And
text.text = "Your text";

Related

enable/disable Objects in Unity

I am totally new in programmation and unity, so I have hard time with basically everything!
Here is my issue : I have a 2D static game with a grid of boxes. each box is made of buttons to click.
I want all the boxes but one not visible at the beginning, and then the box have a button to make boxes appears one by one.
here is my code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OpenBox : MonoBehaviour
{
// Start is called before the first frame update
private GameObject boite1;
void Start()
{
box1 = GetComponent<Box1> ();
}
void Update()
{
if (Input.GetKeyUp(KeyCode.Space))
{
box1.enabled = true;
}
}
}
The "Box1" is underline in red with message : CS0246, The type or namespace name could not be found.
I am not sure I know how to refer to the game object.
thank you for your help !
try use
gameObject.SetActive(false);
more detail unity doc

Can I use a variable I got from the player into a text box?

I'm trying to make an app in Unity which requires your name and other details. I'm using the legacy Input Field to get the answers from the player which worked, but I can't seem to use the Input in any way other than Debug.Log.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ReadInput : MonoBehaviour
{
private string input;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void ReadStringInput(string s)
{
input = s;
Debug.Log(input);
}
}
I used this simple script to get the input but I have no clue how to actually use it in the game. It want it to say, for example, Welcome,(Person's Name/Input) outside of the log and actually into a text object.
If you right click in the hierarchy, under UI you can add a text object. I like to use a free package called Text Mesh Pro, which you can get in the asset store, but you don't need to.
Once you've made your text object, it should appear as a child of a gameobject called the Canvas. The text object should have a text field that you can access from script.
Text mytext;
public void UpdateMyText(string s)
{
mytext.text = s;
}
Here's the documentation
Just drag the text object into the script's mytext field in the inspector, and call that function when you want to update your text.

I'm trying to use a code to display score in the UI text but it didn't show up/change the text object

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class wastafel : MonoBehaviour
{
public Text ctText;
public static int iscucitangan;//scoring
public int handwash;
void update()
{
handwash = iscucitangan;
ctText.text = "Sudah cuci tangan :" + handwash;
}
}
I tried this script to display a scoring system I made in unity (I use this video as reference : https://www.youtube.com/watch?v=YKeXyaB41EA). I applied this code to a text UI object in my program like this. This is the CT text object. But when I press play, it came up like this, no change at all.
This is my text setting. Is there something that I miss?
It is caused because of the typo, it should be Update() with a capital U and not update().

Unity changing text with script c#

I'm trying to add a scoring method in my Unity phone game by changing the UI text.
Here is My code:
using UnityEngine;
using UnityEngine.UI;
public class ScoreDisplay : MonoBehaviour
{
// Start is called before the first frame update
[SerializeField]private Text m_MyText;
void Start()
{
m_MyText.text = "This is my text";
}
// Update is called once per frame
void Update()
{
m_MyText.text = "UWU";
}
}
Nothing pops up when I search for a text object:
And I cannot drag the mesh text into the public variable. Any help would be appreciated, thank you.
Text is a legacy element and is mostly unused. Instead, you want Text Mesh Pro's text component which is under the TMPro namespace. The componant is called TextMeshProUGUI.
Replace
[SerializeField]private Text m_MyText;
with
[SerializeField]private TMPro.TextMeshProUGUI m_MyText;
The TextMeshProUGUI componant has a text field, so your code should work as normal after replacing the line.

Change a Text dynamically using PointerEnter

As I'm currently developing a Unity Engine based Game right now I need a PointerEnter EventTrigger to change my Text dynamically. Specifically:
If the user hovers with the Mouse in the MainMenu over a Text, I want an indicator on which Option he is pointing at.
So from Options the Text should turn to ▶ Options.
What I did is the following:
Text text;
string ContinueText = "▶ Continue";
void Awake()
{
// Set up the reference.
text = GetComponent<Text>();
}
public void Test()
{
text.text = ContinueText;
}
But if I hover over the Text I get
NullReferenceException: Object reference not set to an instance of an object
pointing at text.text = ContinueText;
So I searched around the Web and found that void Update() is sometimes called before Awake(), the Error stays the same anyway. The Canvas-Text is named "Text_Options", in case you need that.
Thanks for helping me out!
Here is an working example.
A canvas with an empty gameobject (has a vertical layout group, but thats not relevant) that is a container for two text objects.
I've added two event triggers each, OnPointerEnter and OnPointerExit. Both text objects have my script HoverText on them:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class HoverText : MonoBehaviour
{
Text text;
public string content = "Text A";
public string contentHighlighted = "▶ Text A";
void Awake()
{
text = GetComponent<Text>();
text.text = content;
}
public void Highlight()
{
text.text = contentHighlighted;
}
public void UnHighlight()
{
text.text = content;
}
}
Text_A has itself as gameobject for it's both event triggers and Text_B itself respectively. The public strings for the two different text contents are set via inspector (default value from script is actually matching Text_A in my example).
That's it, works fine.

Categories