【发布时间】: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