【问题标题】:Add guitext in to gameobject将guitext添加到游戏对象
【发布时间】:2014-06-04 18:39:58
【问题描述】:

我正在使用 smartfox server2x 在 unity3d 中进行多人协作。在这有2个脚本 a)game manager.cs 被拖入游戏对象 b)timer.js 脚本被拖入 guitext 。如何将此 guitext 添加到游戏对象中?当我尝试执行此操作时,我收到了类似

的错误

“没有'GUIText'附加到“Game”游戏对象,但是一个 脚本正在尝试访问它。

您可能需要向游戏对象“Game”添加一个 GUIText。或者你的 脚本在使用之前需要检查组件是否被附加。”

这是一个脚本

#pragma strict
public var timeLeft:float = 10.0f;
public var IsTiming : boolean;

//public bool IsTiming = false;
   public function Start () {
   //IsTiming = false;

 }
 function test1(vr1:boolean) 
 {
 IsTiming=true;
 Debug.Log("enter33333333333"+IsTiming);
//gamemanager = this.GetComponent("GameManager");

}
function OnGUI() 
  {
 Debug.Log("enter2222222222"+IsTiming);
if(GUI.Button(Rect(0, 70, 150, 25), 'counter')) 
{

IsTiming = true;

}
if(IsTiming)
    {
    Debug.Log("enterzzzzzzzzzz");
        timeLeft -= Time.deltaTime;

        if (timeLeft <= 0.0f)

        {

            guiText.text = "You ran out of time";
            guiText.text = "Time is over";


        }  
        else

        {

            guiText.text = "Time left = " + timeLeft + " seconds";

        }

       }
  }

如何将此脚本添加到游戏对象“游戏”中。

【问题讨论】:

    标签: unity3d unityscript


    【解决方案1】:

    该错误将发生,因为您的游戏对象没有GUIText 组件;您需要先添加一个。

    要添加 GUIText 组件,您可以在 Start 函数中编写 gameObject.AddComponent(GUIText)

    或者,您可以通过 Unity 编辑器菜单添加一个 GUIText 组件,方法是转到 Component > Add...,然后输入 GUIText

    另外,在脚本中写 guiText 是写 GetComponent(GUIText) 的简写!

    【讨论】:

    • 但我想在游戏对象中编写脚本。
    • 好的,所以在你的Start函数中,写gameObject.AddComponent(GUIText);,然后你就可以操作guiText变量了。
    • @Chris GuiText 在版本 5 中已弃用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-24
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-06
    相关资源
    最近更新 更多