Unity4开发(跟随Brackeys学习):using UnityEngine.UI;无法使用求助
Fixing Your Unity 4 Score Code Issue
Hey there! Let's get that score display working in Unity 4 — I see exactly where the problems are.
First, the UnityEngine.UI Problem
Unity 4 doesn't have the UnityEngine.UI namespace at all. That was added in Unity 5 with the new uGUI system. You can safely delete that line entirely; it's not doing anything here and might be contributing to confusion.
Second, the GUIText Property Mistake
You're using Unity 4's old GUIText component, but your code is trying to access scoreText.guiText — that's incorrect. The GUIText component has a direct text property you should use instead.
Here's the Corrected Code
using UnityEngine; public class score : MonoBehaviour { public Transform player; public GUIText scoreText; // Update is called once per frame void Update () { scoreText.text = player.position.z.ToString(); } }
Quick Setup Checks to Avoid Other Errors
- Make sure your score GameObject has a GUIText component (not the newer UI Text from Unity 5+ — they're totally different systems in Unity 4).
- Drag your player GameObject into the
playerfield in the inspector for yourscorescript, and drag the GUIText GameObject into thescoreTextfield (no null references allowed!). - Adjust the GUIText's position/anchor settings in the inspector so you can actually see the score on screen.
内容的提问来源于stack exchange,提问作者Blackbird
相关产品推荐
相关产品推荐

