TextMesh Pro text will not change via script - c#

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.

Related

C# - Failed at calling another script

I've created two scripts. One includes a variable and a method. Second script's task is to call the first script and access its component. However I'm getting the following error :
ThisScriptWillCallAnotherScript.Update () (at Assets/Scripts/ThisScriptWillCallAnotherScript.cs:21)
I tried removing the line it's referring to but the error persists.
Any idea what I may be doing wrong?
Script 1 :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThisScriptWillBeCalledInAnotherScript : MonoBehaviour {
public string accessMe = "this variable has been accessed from another script";
public void AccessThisMethod () {
Debug.Log ("This method has been accessed from another script.");
}
}
Script 2 :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThisScriptWillCallAnotherScript : MonoBehaviour {
// below we are calling a script and giving a name//
ThisScriptWillBeCalledInAnotherScript callingAScript;
void Start () {
//here we are using GetComponent to access the script//
callingAScript = GetComponent<ThisScriptWillBeCalledInAnotherScript> ();
Debug.Log ("Please press enter key...");
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.Return)) {
Debug.Log ("this is the script we just called " + callingAScript);
Debug.Log (callingAScript.accessMe); // we are accessing a variable of the script we called
callingAScript.AccessThisMethod (); // we are calling a method of the script we called
}
}
}
It Unity GameObjects can have Components.
The method GetComponent<T>() gets a reference to the component T from the current GameObject.
So if your GameObject has both components (ScriptAand ScriptB)
then this will return a "not-null" reference to the instance of ScriptB:
public class ScriptA : MonoBehaviour {
ScriptB scriptB;
// Use this for initialization
void Start () {
scriptB = GetComponent<ScriptB>(); //Not null if GameObject has ScriptB component.
}
}
If you GameObject does not have the component ScriptB, then the Method GetComponent<T>() will return null.
If ScriptB is a component from another GameObject then you will need a reference to that other GameObject and call it via OtherGamoeObject.GetComponent<T>()
If ScriptB is not even a Script that changed the GameObject and simply (for example) contains some Math-Calculations or so, then I would suggest not making it inherit from Monobehaviourand simply creating an instance like so: var scriptB = new ScriptB();

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.

Enable/disable a GameObject component from a script [Unity3D]

I need to take the value of a boolean (put in a variable called "bouclier") set in one script to enable or disable a GameObject.
The variable is in game object Player (bottom right here):
And I need to enable of disable this game object ("Bouclier01"):
To do this, I attached a script to game object "Bouclier01". Here it is:
using UnityEngine;
using System.Collections;
public class ShowBouclier : MonoBehaviour {
public GameObject Bouclier01;
public bool bouclier;
// Use this for initialization
void Start () {
Bouclier01 = Bouclier01.GetComponent<GameObject>();
}
// Update is called once per frame
void Update () {
Bouclier01.enabled = false;
if (bouclier == true) {
Bouclier01.enabled = true;
}
}
}
I must be missing something, because this comes up with this error message:
Any idea how to properly accomplish this?
You can use GameObject.SetActive() function to activate or deactivate a GameObject (I think GameObject.enabled was in the old API):
Bouclier.SetActive(false);
By the way, if you want to know the current activation state of a GameObject, use GameObject.activeSelf, which is a read only variable:
Debug.Log(Bouclier.activeSelf);
it will works
public GameObject otherobj;//your other object
public string scr;// your secound script name
void Start () {
(otherobj. GetComponent(scr) as MonoBehaviour).enabled = false;
}

Character not attacking via Unity

So I have built an attack using SpriteFactory, and simply want to assign the keyboard letter A as a default attack. I have used GetKeyUp for the sole purpose of the character to attack once, and not multiple times like a loop (i.e. GetKeyDown). At this stage I have not included any enemies or anything, as i just want the the character to simply attack when I Press A. I have Included the game object and added the Attack.cs but no success. Maybe I am complete missing the point of what I am doing, but some help in the right direction would be appreciated.
using UnityEngine;
using System.Collections;
public class Attack : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKeyUp(KeyCode.A)) {
}
}
}
Ok so i have tried the below method. Have adjusted the code as follows:
using UnityEngine;
using System.Collections;
using FactorySprite = SpriteFactory.Sprite;
public class Attack : MonoBehaviour {
// you forgot to set name of variable representing your sprite
private FactorySprite sprite;
// Use this for initialization
void Start () {
sprite = GetComponent<FactorySprite> (); // Edited
}
void Update ()
{
if(Input.GetKeyUp(KeyCode.A))
{
sprite.Play("Attack");
}
}
}
But now have a ''nullReferenceError'' Object reference not set to an instant object?
You have to add yourSprite.Play("AnimationName"); to your loop:
void Update ()
{
if(Input.GetKeyUp(KeyCode.A))
{
yourSprite.Play("AnimationName");
}
}
For this to work you have to add a reference to your sprite to the script, with GetComponent().
Btw, GetKeyDown only triggers once for each key-press, you can use this too. (GetKey() would fire every frame)

Categories