Unity : Can't drag GameObject with Text on Script reference - c#

I'm trying to change the Text of an UI GameObject with a script but Unity doesn't let me drag the GameObject with the text on the script reference and when i do public GameObject it says GameObject is not a Text.
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Security.Cryptography.X509Certificates;
using UnityEngine;
using UnityEngine.UI;
public class GhostName : MonoBehaviour
{
public Text NameText;
void Start()
{
NameText.text = "The Name is : ";
}
}

You need to store NameText as a reference to a GameObject, and then call GetComponent<Text>() to get the Text component on the object. This way, you'll be able to drag the game object into the 'NameText' field in the Unity Inspector window.
...
public class GhostName : MonoBehaviour
{
public GameObject NameText;
void Start()
{
NameText.GetComponent<Text>().text = "The Name is : ";
}
}
You should also check that GetComponent<Text>() is not equal to null before setting the value of .text.

If anyone still get this issue, I had to change from "TextMeshPro" to "Text" Object.

Disclaimer: I am totally new to Unity. But it seems I have the same issue.
Under Hierarchy -> right click -> UI, there is only "Text - TextMeshPro" option in my Unity version (2021.3.4f1). I don't know why "Text" is missing. (anyone concur with me?)
So I fail to follow this step in a beginner tutorial, I anyway just select TextMeshPro instead. Then I just cannot drag to script. I guess it is because the tutorial is using Text type not TextMeshPro and they are not compatible.
Later I realize that, to enable the drag action, one cannot use [SerializeField] Text xxx; but must use [SerializeField] TextMeshProUGUI xxx;.
Pls also include using TMPro; in the header.

I used TMPro namespace and public TextMeshProUGUI scoreText;
and I was able to drag and drop the Text (TMP) to the script as below:
using TMPro;
using UnityEngine;
public class className : MonoBehaviour
{
// Start is called before the first frame update
public TextMeshProUGUI scoreText;
void Start()
{
scoreText.text = "Score: "+ points;
}

Related

The script is missing a checkbox

I was programming a script and when I want to add it to GameObject, I notice that the enable check disappeared and can't to drag to scripts to the inspector for this component Event Script.
I tried change the names of the scripts but there aren't changes.
The script to add:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EventScript : MonoBehaviour
{
private GameObject obj;
public SaveObjScript saveObjScript;
public SpawnScript spawnScript;
public void InitObject(){
Debug.Log("OBJETO A ACCIONAR ES: " + saveObjScript.getObject().name);
//obj = saveObjScript.getObject();
}
public void ChangeColor(){
InitObject();
Renderer rend = obj.GetComponent<Renderer>();
rend.material.color = Random.ColorHSV();
}
public void RemoveObj(){
InitObject();
Destroy(obj);
spawnScript.mTotalObjetos --;
}
}
It has no checkbox because it doesn't implement any built-in methods like Start or Update or FixedUpdate that care about whether the component is enabled.
From the MonoBehaviour documentation:
Note: There is a checkbox for disabling MonoBehaviour on the Unity Editor. It disables functions when unticked. If none of these functions are present in the script, the Editor does not display the checkbox.

How to pass data from one scene to another?

I want to take username through InputField in scene 1 and access the username entered by the user in scene 2. I have a InputField and a submit button in scene 1. When click the submit button, the scene changes. How should I go about it?
I tried using DontDestroyOnLoad() but this didn't worked for me.
To change the scene i am using SceneManager.LoadScene(name);
Here I came up with this.I achieved what you wanted with the using PlayerPrefs
I created a InputField and attached a script to it called Save and then I called SaveText() from that script after On End Event in inspector
Save.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Save : MonoBehaviour
{
InputField userNameField;
void Start()
{
userNameField = GetComponent<InputField>();
}
public void SaveText()
{
PlayerPrefs.SetString("userName", userNameField.text);
Debug.Log(userNameField.text);
}
public void MoveScene(string sceneToLoad)
{
SceneManager.LoadScene(sceneToLoad);
}
}
and in next Scene I attached the script called Load.cs to a text gameobject and in Start() I got information of that String that saved previously
Load.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Load : MonoBehaviour
{
Text userNameText;
void Start()
{
userNameText = GetComponent<Text>();
userNameText.text = PlayerPrefs.GetString("userName");
}
}
Note : I don't know how efficient this is, But it does the work

TextMesh Pro text will not change via script

I cannot seem to change my TextMeshPro value via script.
In my inspector I have a TextmeshPro object named Countdown. I have a script named GameController which is attached to this.
My script then sets the string value of Countdown to Hello but it does not work.
GameController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class GameController : MonoBehaviour {
public TextMeshProUGUI Countdown;
// Use this for initialization
void Start () {
Countdown = GetComponent<TextMeshProUGUI> ();
Countdown.text = "Hello";
}
// Update is called once per frame
void Update () {
}
}
In the inspector there is a field for TextMesh but I cannot drag the CountDown object to this for some reason, could that be the issue?
the problem is that you are using a regular TextMeshPro object, and in your code your looking for a TextMeshProUGUI, simple mistake. change code to:
public class GameController : MonoBehaviour {
public TextMeshPro Countdown;
// Use this for initialization
void Start () {
//you shouldnt need to get component the editor should take care of this for you when
//you drop it since you have the object set to TextMeshPro and not just GameObject
Countdown = GetComponent<TextMeshPro> ();
Countdown.text = "Hello";
}
// Update is called once per frame
void Update () {
}
}
the only way to make a TextMeshProUGUI object is to add it through a canvas. in your scene when you just add a TMP it will be Regular TMP which your "countdown" is. you can tell because it uses the TMP script not the TMPUGUI script.

Why in Editor type script it's never get to the break point?

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class GetPrefabs : Editor
{
public GameObject[] prefabs;
// Start is called before the first frame update
void Start()
{
prefabs = (GameObject[])Resources.LoadAll("Assets/Test/Animations/");
}
// Update is called once per frame
void Update()
{
prefabs = (GameObject[])Resources.LoadAll("Assets/Test/Animations/");
}
}
First I tried to put the script inside Assets/Test/Editor but it didn't work then I moved the script to Assets/Editor but it's not working either it's never get to the break point I put on the line in the Update or in the Start.
GetPrefabs derives from Editor. The Start() and Update() are MonoBehaviour methods (i.e. Unity looks for them if you derive from MonoBehaviour). You should look at Unity docs for the Editor class and pick appropriate methods from its list - https://docs.unity3d.com/ScriptReference/Editor.html

Unity4 Can't use using UnityEngine.UI ;

I am using Unity4 to create which I learned from Brackeys
My code isnt working, using UnityEngine.UI; isn't working in unity4 i think.
help me.
using UnityEngine;
using UnityEngine.UI;
public class score : MonoBehaviour {
public Transform player;
public GUIText scoreText;
// Update is called once per frame
void Update () {
scoreText.guiText = player.position.z.ToString();
}
}
scoreText.guiText is a type of component not string and it is a read-only variable. That should be scoreText.text because you are trying to modify the text of the GUIText.
public Transform player;
public GUIText scoreText;
// Update is called once per frame
void Update()
{
scoreText.text = player.position.z.ToString();
}
Note that Unity4 is old and GUIText is now deprecated. I suggest you upgrade your Unity version and take advantage of the new UI system which uses the Text component.

Categories