繁体   English   中英

错误 CS0117:“级别”不包含“实例”的定义

[英]error CS0117: 'Level' does not contain a definition for 'Instance'

我是C# 新手 想开发游戏。 Unity 已经改变了一些语法,比如 Application。 无法解决错误。检查了所有拼写。 实例如何在这里工作。

尝试在 Unity 文档和谷歌中搜索。 它来自我正在学习的书。在谷歌中可用。 如果我们输入代码,它就会给出这本书。

块引用

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using RunAndJump;
 using UnityEditor;

    [ExecuteInEditMode] public class SnapToGridTest : MonoBehaviour {
 // Update is called once per frame
    private void Update()
     {
         Vector3 gridCoord = 
              Level.Instance.WorldToGridCoordinates(transform.position);
         transform.position = 
              Level.Instance.GridToWorldCoordinates((int)gridCoord.x,
         (int)gridCoord.y);
     }

     private void OnDrawGizmos()
     {
         Color oldColor = Gizmos.color;
         Gizmos.color = (Level.Instance.IsInsideGridBounds(transform.position)) 
                         ? Color.green
                                     : Color.red;
         Gizmos.DrawCube (transform.position, Vector3.one * Level.GridSize);
         Gizmos.color = oldColor;
     } }

块引用

错误 CS0117:“级别”不包含“实例”的定义

似乎您正在尝试将 Level 视为Singleton ,但 Level 的单例尚未实现。

您可以这样实现单例:

private static Level instance;

public static Level Instance 
{
    get 
    {
        if (instance == null)
        {
            instance = FindObjectOfType<Level>();
        }

        return instance
     }
}

当然,如果当前场景中没有一个带有Level组件的GameObject ,它就不会工作。

此外,您目前正在阅读的教程书似乎已将Level作为部分课程。
该指南可能为包含单例的部分Level类制作了另一个脚本。
您可能刚刚错过了它,或者指南尚未涉及该部分。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM