Deon Howard
Deon Howard

Reputation: 19

How do I fix this "Does Not Contain Definition" Error?

So I'm working on a project for class in which, using Unity3D, I'm instatiating fish game objects into a game and making them appear randomly across the background. Currently, I have this code that's supposed to make my critter appear. I wrote two scripts that are supposed to make my critter game objects appear on different sides of the screen

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 

//same thing MonoBehavior does public class Critter : 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);


    }

}

I keep getting compiler errors in Unity that say things like "Wrangler does not contain definition for "halfWidth" when I clearly define halfWidth = 6.

Errors

What do I do to add the definition and fix these errors?

Upvotes: 0

Views: 1918

Answers (1)

Yuri Nudelman
Yuri Nudelman

Reputation: 2943

C# is case sensitive. In Critter code you use:

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

In Wrangler, halfWidth and halfWidth are declared with capital W and H, but here you use halfwidth and halfheight.

About the error in the image - the Wrangler as you posted is correct and should compile, however pay attention that the error is about "CrabPrefab" and not "crabPrefab" as you declared it, so maybe in your local code you used capital CrabPrefab and CritterPrefab instead?

Just pay attention to the case of your variables.

Upvotes: 1

Related Questions