【问题标题】:How do I fix this "Does Not Contain Definition" Error?如何解决此“不包含定义”错误?
【发布时间】:2015-11-05 02:33:32
【问题描述】:

所以我正在开发一个类项目,在该项目中,我使用 Unity3D 将鱼游戏对象设置为游戏,并让它们随机出现在背景中。目前,我有这段代码应该让我的小动物出现。我写了两个脚本,应该让我的小动物游戏对象出现在屏幕的不同侧面

using UnityEngine;
using System.Collections;

public class Wrangler : MonoBehaviour {
    public static float halfWidth = 6;
    public static float halfHeight = 5;
    public GameObject critterPrefab;
    public GameObject crabPrefab;
    int critterCount = 0; //how many we've made so far
    int critterMax = 5; //how many you want
    int crabCount = 0;
    int crabMax = 5;
     // how long between critter spawns

    void Start()
    {
        StartCoroutine("SpawnCritter");
        StartCoroutine("SpawnCrab");
    }

    IEnumerator SpawnCritter()
    {
        while (critterCount < critterMax)
        {
               CreateCritter();
               yield return new WaitForSeconds(1.0f);
        }                
    }

    IEnumerator SpawnCrab()
    {

        while (crabCount < crabMax)
        {
                CreateCrab();
                yield return new WaitForSeconds(1.0f);
        }       
    }

    void CreateCritter() { 
            critterCount++;
            Instantiate(critterPrefab);
    }


    void CreateCrab() { 
            crabCount++;
            Instantiate(crabPrefab);
    }
}

using UnityEngine; //allows us to use objects and stuff from Unity Engine
using System.Collections;

    //creates a class we can manipulate called "Critter" and allows it      to do the 

//MonoBehavior 做同样的事情 公共类小动物:MonoBehaviour {

    //variables here are for the class to use and functions can use them any time
    //It looks like you can create variables first, then create the functions
    protected Rigidbody rb;

    // Use this for initialization
    public virtual void Start () {

            float startX = Random.Range (-Wrangler.halfWidth, Wrangler.halfwidth);
            float startY = Random.Range (-Wrangler.halfHeight, Wrangler.halfheight);
            transform.position = new Vector2 (startX, startY);


            rb = GetComponent<Rigidbody> ();// Functions make classes do stuff
            setVelocity ();


    }

    public virtual void setVelocity() {
            rb.velocity = new Vector2 (-1,0);
    }

    // Update is called once per frame
    void Update () {

            if (transform.position.x > Wrangler.halfWidth)
                    transform.position = new Vector2 (-Wrangler.halfWidth, transform.position.y);

            if(transform.position.x < Wrangler.halfWidth)
               transform.position = new Vector2 (Wrangler.halfWidth, transform.position.y);

            if(transform.position.y > Wrangler.halfHeight)
                    transform.position = new Vector2 (-Wrangler.halfHeight, transform.position.y);

            if(transform.position.y < Wrangler.halfHeight)
                    transform.position = new Vector22 (Wrangler.halfHeight, transform.position.y);


    }

}

我在 Unity 中不断收到编译器错误,当我明确定义 halfWidth = 6 时,会说“Wrangler 不包含“halfWidth”的定义。

如何添加定义并修复这些错误?

【问题讨论】:

  • 您发布的图片抱怨缺少预制件,而不是班级成员...
  • 您是否将“Crab”-prefab 分配给编辑器中的对应成员?

标签: c# unity3d


【解决方案1】:

C# 区分大小写。 在您使用的 Critter 代码中:

float startX = Random.Range (-Wrangler.halfWidth, Wrangler.halfwidth);
float startY = Random.Range (-Wrangler.halfHeight, Wrangler.halfheight);

在 Wrangler 中,halfWidth 和 halfWidth 用大写的 W 和 H 声明,但这里使用的是 halfwidth 和 halfheight。

关于图像中的错误 - 您发布的 Wrangler 是正确的并且应该编译,但是请注意错误是关于“CrabPrefab”而不是您声明的“crabPrefab”,所以可能在您使用的本地代码中改为大写 CrabPrefab 和 CritterPrefab?

请注意变量的大小写。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-19
    • 2021-12-16
    • 2015-03-23
    • 2020-04-27
    • 1970-01-01
    • 2022-07-06
    • 1970-01-01
    相关资源
    最近更新 更多