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.
Related
I want that whenever my player passes through a particular portion of my obstacle it should add 2 points to the score. In order to do this I've made a child of the obstacle. This child contains the box collider which covers that particular portion of the obstacle (I've switched on the Is Trigger in Unity).
Code on child having trigger -
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Score : MonoBehaviour
{
float points;
void Start()
{
}
void Update()
{
Debug.Log(points);
}
void OnTriggerExit2D(Collider2D other)
{
points += 2f;
}
}
The problem is that in the console the points are showing 0s and 2s only like this -
Console
While it should be 0, 2, 4, 6... after passing the obstacle.
Also clones of the original obstacle are being created, i.e. I pass through a new clone each time; in case this is causing the problem.
Yeah it is obvious that it will print out 0s and 2s because your script doesn't use a centralized scoring system it just updates the score in that particular instance. You can make another script call it GameManager where you can make a player score variable and on trigger exit function of your child function would increment the score of that GameManager variable playerscore.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour
{
public float playerPoints = 0f;
}
In Score Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Score : MonoBehaviour
{
public GameManager manager; // Drag and drop the gamemanager object from scene in to this field
void OnTriggerExit2D(Collider2D other)
{
manager.playerPoints += 2f;
Debug.Log(manager.playerPoints);
}
}
I feel like this is a poor question to ask, but I couldn't figure out what the issue was.
I was making a ScoreController and I wanted to make it so that a coin object, when hit, would add 100 to the score GUI. All the coins have this script inside of them:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CoinScoreController : MonoBehaviour
{
public void OnTriggerEnter2D(Collider2D collider)
{
transform.parent.parent.parent.Find("Canvas/ScoreUIController").CollectCoin();
}
}
I can recognize that this is a very poor way of getting an object but I can't figure out what the issue is. Here is the script that I was trying to reference:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScoreController : MonoBehaviour
{
public static int score = 0; //Number of coins
public static GameObject scoreUI; //A gameObject for the CoinUI
// Update is called once per frame
void Start()
{
scoreUI = GameObject.Find("ScoreUI"); //Automatically sets all coins to have their coinUI GameObject equal the CoinUI UI.
}
public void CollectCoin() //Runs when a coin is collected. Makes it disapear and adds one to the counter.
{
gameObject.SetActive(false);
score += 100;
scoreUI.GetComponent<Text>().text = "Score: " + score.ToString();
}
public void KilledMonster()
{
score += 1000;
}
}
The ScoreControllerUI is inside Canvas so I don't think I have the wrong path. Can you not transform.Find() from the scene? Here is my scene:
As I am sure you can tell, I am new to this site and to Unity, so I apologize for any mistakes in the way I asked this question. Feel free to give me constructive criticism on that as well. I am using unity version 2020.3.24f1 and coding in C#. I don't know if this is relevant but I am coding using visual studio.
Sorry again for any dumb mistakes I made.
Instead of using
transform.parent.parent.parent.Find("Canvas/ScoreUIController").CollectCoin();
use
GameObject.Find("Canvas/ScoreUIController").GetComponent<ScoreController>().CollectCoin();
Suggestion: Define a public ScoreController ScoreUIController in your CoinScoreController (I'm assuming your coin is a prefab) then drag and drop the ScoreUIController into it to assign your script to our defined variable.
Now, you would be able to call
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CoinScoreController : MonoBehaviour
{
public ScoreController ScoreUIController;
public void OnTriggerEnter2D(Collider2D collider)
{
ScoreUIController.CollectCoin();
}
}
P.S. : Please follow tutorials and read documentation first, as I can see your code is very process heavy.
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;
}
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.
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