如何避免Unity 2D中产卵对象的对象引用?

问题描述 投票:0回答:1

目前我正在编程一个Unity 2D游戏。当游戏运行时,汽车开始移动并不断地重生。我添加了一种生命系统,以实现射击汽车的可能性。我的问题是,我的健康栏和记分板需要引用它们所引用的对象,但我无法创建一个在游戏开始前不存在的对象的引用。另一个问题是,我不知道如何将画布添加到预制件中,以便将其与汽车连续生成,并将其与汽车连接起来。有没有一种方法可以避免这些冲突,或者如何将引用设置成预制件。我将添加生成器,汽车和记分牌的代码。已经提前感谢你

生殖器。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Spawner : MonoBehaviour
{
  public GameObject carPrefab;
  public GameObject enemyCarPrefab;
  public GameObject kugel;
  public float respawnTime = 10.0f;
  public int counterPlayer1=0;
  public int counterPlayer2=0;
  public int counterEnergy=0;

  // Use this for initialization
  void Start () {
      StartCoroutine(carWave());
  }
  private void spawnPlayerCars(){
      GameObject a = Instantiate(carPrefab) as GameObject;
      a.transform.position = new Vector2(-855f, -312.9426f);
      counterPlayer1++;
  }
  private void SpawnEnemyCars(){
      GameObject b = Instantiate(enemyCarPrefab) as GameObject;
      b.transform.position = new Vector2(853,-233);
      counterPlayer2++;
  }
  private void SpawnEnergy(){
      GameObject c = Instantiate(kugel) as GameObject;
      c.transform.position = new Vector2(-995,-192);
      counterEnergy++;
  }


  IEnumerator carWave(){
      while(true){
          yield return new WaitForSeconds(respawnTime);
          if(counterPlayer1<3){
          spawnPlayerCars();
          Debug.Log(counterPlayer1);
          }
          if(counterPlayer2<3){
          SpawnEnemyCars();
          Debug.Log(counterPlayer2);
          }
          if(counterEnergy<3){
          SpawnEnergy();
          }
      }
  }


}

汽车

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyCar : MonoBehaviour
{
    public float speed = 3f;
    int zählerAuto1=0;
    private Vector2 screenBounds;
    public AnzeigePunktzahlPlayer2 points;
    public Spawner sp;
    public int maxHealth=100;
    public int currentHealth;
    public HealthBar healthbar;

    void Start () {
    screenBounds = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, Screen.height));
    points= GetComponent<AnzeigePunktzahlPlayer2>();
    sp= GetComponent<Spawner>();
    currentHealth=maxHealth;
    healthbar.SetMaxHealth(maxHealth);



    }
    void Update()
    { 
    Vector2 pos = transform.position;
        if(pos.x>-855f){
        pos = transform.position;
        pos.x-= speed* Time.deltaTime;
        transform.position=pos;
        zählerAuto1++;
        }else{
            points.counter++;
            Debug.Log(points.counter);
            sp.counterPlayer2--;
            Debug.Log(sp.counterPlayer2);
            Destroy(this.gameObject);        
        }
    }
    private void OnCollisionEnter2D(Collision2D other) {
        if (other.collider.tag=="Kugel"){
            takeDamage(40);
            //sp.counterPlayer2--;

            if(currentHealth<=0)
            {
            Destroy(this.gameObject);
            }
        }
}
public void takeDamage(int damage){
    currentHealth-= damage;
    healthbar.SetHealth(currentHealth);
}

public void getHealed(int heal){
    currentHealth+= heal;
    healthbar.SetHealth(currentHealth);
}
}

记分牌(其中一部分(另一部分几乎一样))。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class AnzeigePunktzahlPlayer1 : MonoBehaviour
{
    public int counter;
    public TextMeshProUGUI textPlayer1;

    void Start()
    {
 //   counter=0; 
    textPlayer1= GetComponent<TextMeshProUGUI>();
    }

    // Update is called once per frame
    void Update()
    {
        textPlayer1.SetText( counter.ToString());
    }
}



unity3d reference spawn
1个回答
0
投票

你可以把健康棒和帆布的孩子的汽车预制,并让他们一起产卵。

© www.soinside.com 2019 - 2024. All rights reserved.