【发布时间】:2020-08-10 06:39:27
【问题描述】:
我创建的这个简单游戏围绕着玩家收集不同的拾取物来得分。我在拾取的 GameObject 上实现了协程,因此它们每隔几秒就会有规律地生成。
但是,我创建的拾取器在第二次生成后无法使用。这个名为 Jeans 的 GameObject 是一个拾取物,玩家收集这些拾取物来得分。
但是,第二个和后续的拾取器生成的所有“对应”都与第一个生成的拾取器“对应”。意思是,如果有 3 个生成的游戏对象实例,如果我要收集预制件 2 或 3,游戏对象 1 将消失并添加分数。但是,无法收集游戏对象的未来生成。玩家只会通过它们。
这些是代码:
Consumable.cs
public class Consumable{
// declare a variable of type 'items'
// using autoproperties {get; private set;}
// cannot be initialised in the beginning
public items type{get; private set;}
public float damage{get; private set;}
public float health{get; private set;}
// create the constructor
public Consumable (items type, float damage, float health){
this.type = type;
this.damage = damage;
this.health = health;
}
}
裤子.cs
public class Pants : Consumable
{
public int amount{get ; private set;}
public GameObject [] pants;
private int index = 0;
public Pants(items type, int amount, float damage = 0, float health = 0) : base (type, damage, health){
this.amount = amount;
pants = new GameObject[amount];
ItemManager.onHit += hit;
}
public void add(GameObject pant){
if (index < amount){
pant.GetComponent<BasicObjectScript>().index = index;
pant.GetComponent<BasicObjectScript>().type = type;
pants[index] = pant;
index ++;
}
}
public void hit(items type, int index){
if (type == this.type){
pants[index].SetActive(false);
amount --;
}
}
}
ItemManager.cs
public class ItemManager : MonoBehaviour
{
private Pants jeans;
public GameObject JeansPrefab;
void Start(){
…
jeans = new Pants(Consumable.items.JEANS, Random.Range(1, 1), 0 ,15);
StartCoroutine(CreateObject());
}
…
private void instantiateJeans(){
jeans.add(Instantiate(JeansPrefab, new Vector2(Random.Range(-rangeX, rangeX), Random.Range(-rangeY, rangeY)), Quaternion.identity));
}
IEnumerator CreateObject(){
while(true){
yield return new WaitForSeconds(Random.Range(1f, 2f));
instantiateJeans();
}
}
...
public void consumablesHit(float speed, Consumable.items type, int index){
Debug.Log("consumables Hit!" + " of type " + type.ToString());
if (onHit != null){ //check if the event has any listeners
onHit(type, index); //if yes, call them
}
// score update
if (type == Consumable.items.JEANS){
score += (int) jeans.health;
}
调用 hit 方法的 BasicObjectScript:
public class BasicObjectScript : MonoBehaviour, BasicObjectInterface{
public int index;
public Consumable.items type;
public GameObject ItemManager;
//this is the method that one has to implement (because of the adopted BasicObjectInterface)
public void getHit(float speed){
ItemManager = GameObject.FindGameObjectWithTag("ItemManager");
// call the ItemManager's consumables hit method, and the item manager will notify everybody
ItemManager.GetComponent<ItemManager>().consumablesHit(speed, type, index);
}
}
【问题讨论】:
标签: unity3d