【问题标题】:Unity (Object name vs gameobject)Unity(对象名称与游戏对象)
【发布时间】:2017-12-13 00:39:21
【问题描述】:

我正在参加一个使用 Unity 进行游戏开发的在线课程,讲师有时可能会含糊其辞。我的印象是使用游戏对象与使用游戏对象名称(在本例中为 MusicPlayer)相同,但是当我尝试用游戏对象实例替换 MusicPlayer 实例时,我收到错误 CS0246: The type or namespace name `gameobject ' 找不到。您是否缺少 using 指令或程序集引用?我只是想了解两者之间的区别。提前谢谢你。

using UnityEngine;
using System.Collections;
public class MusicPlayer : MonoBehaviour {
static MusicPlayer instance = null;
 void Awake(){
    if (instance != null){
    Destroy(gameObject);
    Debug.Log("Duplicate MusicPlayer Destroyed");
    }

    else{
    instance = this;
    GameObject.DontDestroyOnLoad(gameObject);
    Debug.Log("Original MusicPlayer Created!");
    }
}
}

这是我遇到错误的代码:

using UnityEngine;
using System.Collections;
public class MusicPlayer : MonoBehaviour {
public static gameobject instance = null;

void Awake(){
    if (instance != null){
    Destroy(gameObject);
    Debug.Log("Duplicate MusicPlayer Destroyed");
    }

    else{
    instance = this;
    GameObject.DontDestroyOnLoad(gameObject);
    Debug.Log("Original MusicPlayer Created!");
    }
}
}

【问题讨论】:

  • 有什么区别?还要添加您遇到错误的代码。
  • 代码在吗?我错过了什么吗?
  • 我现在明白你的意思了,我想知道为什么我不能使用gameObject,必须使用gameObject的名称。 (显示在两个代码的第 4 行)
  • 请检查我的答案。它解决了您的错误,并向您展示了为什么使用第一个代码而不是第二个代码。

标签: c# unity3d scripting monodevelop


【解决方案1】:

gameObjectGameObject 之间存在差异。注意第二个中大写的“G”。

GameObject 是一个类。你可以像这样创建它的一个实例:

GameObject myobject = new GameObject("PhilipBall");

或者您可以将其设为公共变量并从编辑器中分配:

public  GameObject myobject;

gameObject 是一个从 GameObject 创建的变量。它的声明类似于上面的 myobject 变量示例。

您看不到它的原因是因为它是在名为Component 的类中声明的。然后一个名为Behaviour 的类继承自Component 类。还有另一个名为MonoBehaviour 的类继承自Behaviour 类。最后,当您执行 public class MusicPlayer : MonoBehaviour 时,名为 MusicPlayer 的脚本继承自 MonoBehaviour。因此,您继承了 gameObject 变量并可以使用它。


GameObject 类的gameObject 变量类型,用于引用此脚本附加到的 GameObject。

我想知道为什么我不能使用 gameObject 并且必须使用 游戏对象

您实际上可以做到这一点。只需将public static gameobject instance = null; 替换为public static GameObject instance = null;,然后将instance = this; 替换为instance = this.gameObject;

public class MusicPlayer : MonoBehaviour
{
    public static GameObject instance = null;

    void Awake()
    {
        if (instance != null)
        {
            Destroy(gameObject);
            Debug.Log("Duplicate MusicPlayer Destroyed");
        }

        else
        {
            instance = this.gameObject;
            GameObject.DontDestroyOnLoad(gameObject);
            Debug.Log("Original MusicPlayer Created!");
        }
    }
}

当你使用这个时,你指的是这个脚本,它是MusicPlayer。当您使用this.gameObject 时,您指的是这个脚本附加到的游戏对象。

为什么你的导师没有使用public static GameObject instance = null;

这是因为他们想在运行时访问MusicPlayer 脚本变量和函数。他们不需要游戏对象。现在,这可以通过 GameObject 完成,但您必须执行额外的步骤,例如使用 GetComponent 才能在该脚本中使用变量或调用函数。

例如,您在该脚本中有一个名为“runFunction”的函数,并且您想调用它。对于第一个示例,您可以这样做:

MusicPlayer.instance.runFunction();

对于第二个例子,你必须这样做:

MusicPlayer.instance.GetComponent<MusicPlayer>().runFunction();

这是最大的区别,还要注意GetComponent 很贵。

【讨论】:

  • 非常感谢,我现在明白了。
猜你喜欢
  • 2016-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多