You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 player field in the inspector for your score script, and drag the GUIText GameObject into the scoreText field (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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:20:10