0
因此,我正在開發一個課程項目,其中使用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分配給編輯器中的coresponding成員? – AntiHeadshot