如何修复此“不包含定义”错误?

时间:2015-11-05 02:33:32

标签: c# unity3d

所以我正在开发一个类的项目,在这个项目中,使用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“的定义。

Errors

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

1 个答案:

答案 0 :(得分: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。

关于图像中的错误 - 您发布的牧马人是正确的并且应该编译,但要注意错误是关于&#34; CrabPrefab&#34;而不是&#34; crabPrefab&#34;正如你所声明的那样,也许在你的本地代码中你使用了大写的CrabPrefab和CritterPrefab?

请注意变量的情况。