- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在参加使用 Unity 进行游戏开发的在线类(class),讲师有时会含糊不清。我的印象是使用游戏对象与使用游戏对象名称(在本例中为 MusicPlayer)相同,但是当我尝试将 MusicPlayer 实例替换为游戏对象实例时,我得到错误 CS0246:类型或命名空间名称`游戏对象' 找不到。您是否缺少 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
是有区别的。注意第二个中大写的“G”。
GameObject是一个类。您可以像这样创建它的实例:
GameObject myobject = new GameObject("PhilipBall");
或者您可以将其设为公共(public)变量并从编辑器中分配它:
public GameObject myobject;
gameObject
是一个从 GameObject 创建的变量。它的声明类似于上面的 myobject
变量示例。
您看不到它的原因是因为它是在名为 Component
的类中声明的。然后一个名为 Behaviour
的类继承自该 Component
类。还有一个名为 MonoBehaviour
的类继承自 Behaviour
类。最后,当您执行 public class MusicPlayer : MonoBehaviour
时,名为 MusicPlayer 的脚本继承自 MonoBehaviour
。因此,您继承了 gameObject
变量并可以使用它。
GameObject的gameObject
变量类型类,用于引用此脚本附加到的游戏对象。
I want to know why I cannot use gameObject and must use the name of the 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
时,您指的是此脚本附加到的这个 GameObject。
为什么 public static GameObject instance = null;
没有被你的导师使用?
这是因为他们想要在运行时访问MusicPlayer
脚本变量和函数。他们不需要GameObject。现在,这可以通过游戏对象来完成,但您必须执行额外的步骤,例如使用 GetComponent
才能在该脚本中使用变量或调用函数。
例如,您在该脚本中有一个名为“runFunction”的函数并且您想要调用它。对于第一个示例,您可以这样做:
MusicPlayer.instance.runFunction();
对于第二个例子,你必须做:
MusicPlayer.instance.GetComponent<MusicPlayer>().runFunction();
这是最大的区别,还要注意 GetComponent
的开销很大。
关于c# - 统一(对象名称与游戏对象),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44992383/
我正在为期末考试学习,但我无法理解这个 FC 算法: 我理解你标准化每条规则的部分。然后我认为下一行是说对于满足广义 Modus Ponens (p'_iTheta = p_iTheta) 的每个 t
我有一个 3d 世界,它有一个 simpel 平台和一个代表玩家的立方体。当我旋转平台时,立方体会滑动并按照您预期的方式执行,增加和减少物理 Material 中的摩擦力。 我希望立方体在输入例如 f
所以我的 Unity 项目有一个大问题。我昨天工作,我没有做备份今天,在我打开项目后,我的笔记本电脑因电池电量不足而关机。之后,当我进入项目时,我得到了这个:加载“Assets/MyScene.uni
好的,我正在尝试创建一个函数来确定元组列表是否是可传递的,即如果 (x,y) 和 (y,z) 在列表中,那么 (x,z) 也在列表中。 例如,[(1,2), (2,3), (1,3)]是传递的。 现在
这个问题在这里已经有了答案: How to pass data between scenes in Unity (5 个回答) 9 个月前关闭。 我有一个游戏,我有一个队列匹配系统。 我想向玩家展示他
我现在正在为我的游戏创建一个 keystore (统一)但是当我按下添加键按钮时,会弹出一个错误 Java Development Kit (JDK) directory is not set or
我想将YouTube流视频放入Cardboard(适用于Android和iOS)应用中。我知道这些插件可以执行类似的操作,例如“Easy Movie Texture”,但它们不支持YouTube流媒体
我需要限制 ConfigurableJoint 的目标旋转以避免关节变形或破坏。 为了了解角度限制的工作原理,我做了一个实验。 在场景中放置一个人形模型。 为骨骼添加ConfigurableJoint
尝试实现一种有限形式的匹配统一。 尝试匹配两个公式匹配如果我们能找到替代出现在公式中的变量使得两者在句法上是等价。 我需要写一个函数来判断一个对应于基本项的常数,例如 Brother(George)
我正在使用 Unity 和 C#我想在运行时将输出日志文件发送到我的电子邮件,我使用了来自 this question 的 ByteSheep 答案和来自 this question 的 Arkane
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我希望能够将鼠标悬停在游戏对象(代理)上并在右键或左键单击时创建一个类似于 Windows 右键单击菜单的 float 菜单。我试过结合使用 OnGUI() 和 OnMouseOver() 但我要
我正在为 oculus Gear VR 开发游戏(考虑内存管理),我需要在特定时间(以秒为单位)后加载另一个屏幕 void Start () { StartCoroutine (loadSce
我设法生成了敌人,但它们一直在生成。如何设置限制,避免不断生成? 我已经尝试添加 spawnLimit 和 spawnCounter 但无法让它工作。 var playerHealth = 100;
我正在参加使用 Unity 进行游戏开发的在线类(class),讲师有时会含糊不清。我的印象是使用游戏对象与使用游戏对象名称(在本例中为 MusicPlayer)相同,但是当我尝试将 MusicPla
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 6 年前。 Improve this qu
为了好玩,我正在(用 Java)开发一个使用统一算法的应用程序。 我选择了我的统一算法返回所有可能的统一。例如,如果我尝试解决 添加(X,Y)=成功(成功(0)) 返回 {X = succ(succ(
如何让对象在一段时间后不可见(或只是删除)?使用 NGUI。 我的示例(更改): public class scriptFlashingPressStart : MonoBehaviour {
我有下一个错误: The type or namespace name 'NUnit' could not be found (are you missing a using directive or
这是可以做到的 但是属性 autoSizeTextType 只能用于 API LEVEL >= 26,并且 Android Studio 会显示有关该问题的烦人警告。 为了摆脱这个问题,我想以编程方
我是一名优秀的程序员,十分优秀!