当我死时,一颗心不会被移除,但没有任何错误

时间:2018-12-26 07:49:27

标签: c# unity3d

我正在创建一个2d跑步者,并且我有心脏系统所在的“ collision.cs”文件,但没有出现任何错误,但是当我死了时,心脏没有被移除,所以我是什么做错了吗?

该系统基于标签,因此我尝试了很多更改标签系统但无济于事的事情。然后一段时间后,我试图改变寻找游戏对象的方式,我发现它创造了很多游戏对象,而不是3个,所以我试图改变标签系统,但那还是行不通,我真的没有想要有一个说3条生命等的纯文本。

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

    public class Collision : MonoBehaviour {

        public int lifes = 3    ;
        //public GameObject Life_icon1;
        //public GameObject Life_icon2;
        //public GameObject Life_icon3;
        public  Sprite Heart;
        public  int current_icon = 3;
        public  GameObject ParentPanel; 

        public static int Coins = 0;

        public  float offset = 150f;
        List<int> i = new List<int>() { 1 };

        public int tagN;

        void Update() {
            tagN = i.Max() + 1;
        }

        // Use this for initialization
        void Start () {
        for(int x = 0; x < lifes; x++)
        {
            i.Add(x);
            GameObject NewObj = new GameObject(); //Create the GameObject
            Image NewImage = NewObj.AddComponent<Image>(); //Add the Image Component script
            NewImage.gameObject.tag = x.ToString();
            if(x == 0) {
                NewImage.transform.position = new Vector3(0 + 40f, 0 + 40f , 0);
            } else {
                NewImage.transform.position = new Vector3(0 + offset, 0 + 40f , 0);
                offset += 130f;
            }
            //print(x);
            NewImage.sprite = Heart; //Set the Sprite of the Image Component on the new GameObject
            NewObj.GetComponent<RectTransform>().SetParent(ParentPanel.transform); //Assign the newly created Image GameObject as a Child of the Parent Panel.
            NewObj.SetActive(true); //Activate the GameObject
        }
        }

         void AddLife(int amount) {
            lifes++;
            i.Add(amount);
            GameObject NewObj = new GameObject(); //Create the GameObject
            NewObj.gameObject.tag = tagN.ToString();
            Image NewImage = NewObj.AddComponent<Image>(); //Add the Image Component script
            NewImage.transform.position = new Vector3(0 + offset, 0 + 40f , 0);
            NewImage.sprite = Heart; //Set the Sprite of the Image Component on the new GameObject
            NewObj.GetComponent<RectTransform>().SetParent(ParentPanel.transform); //Assign the newly created Image GameObject as a Child of the Parent Panel.
            NewObj.SetActive(true); //Activate the GameObject
        }


        void DelLife(int amount) {
            Vector3 pos = transform.position;


            offset -= 130f;

            pos.x = -0.49f;
            pos.y = -0.49f;
            transform.position = pos;

            i.RemoveAt(i.Max());

                 GameObject go = null;
                 if (GameObject.FindWithTag(tagN.ToString()) != null)
                 {
                     go = GameObject.FindWithTag(tagN.ToString());
                 }
                 if (go != null)
                 {
                     go.gameObject.SetActive(false);
                 }

        }

        void OnTriggerEnter2D(Collider2D other)
            {          

                //extra life item collision
                if(other.gameObject.name == "Heart_item") {
                        AddLife(1);
                        Destroy(other.gameObject);
                }

                if(other.gameObject.name == "Spike") {
                        DelLife(1);
                    }   

                    if(other.gameObject.name == "Coin") {
                        Destroy(other.gameObject);
                        Coins += 1;
                    }

          }
    }

0 个答案:

没有答案