Unity 2D无法获取完全执行执行的方法

时间:2017-06-29 09:35:03

标签: c# android mobile 2d unity5

嘿伙计们,所以我认识Unity,我一直在为Unity制作教程,并将其转移到移动平台。我已经完成了大部分工作,但问题是我的方法之一似乎没有完全执行,我有三个主要代码。一个控制船只,第二个是我的子弹的代码(这是问题发生的地方),最后一个是一个GameControl用于跟踪分数(似乎没有跟踪分数)代码我的船控制是

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

    public class ShipControl : MonoBehaviour {

        public float playerSpeed = 10f;
        public GameControl gameController;
        public GameObject bulletPrefab;
        public float reloadTime = 1f;
        private bool dragging = false;

        private float elapsedTime = 0;



        void Update()
        {
            elapsedTime += Time.deltaTime;
            if (Input.touchCount > 0) 
            {
                Touch touch = Input.GetTouch(0); // get first touch since touch count is greater than zero

                if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved) 
                {
                    // get the touch position from the screen touch to world point
                    Vector3 touchedPos = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 10));
                    // lerp and set the position of the current object to that of the touch, but smoothly over time.
                    transform.position = Vector3.Lerp(transform.position, touchedPos, Time.deltaTime);
                    if (elapsedTime > reloadTime) {
                        Vector3 spawnPos = transform.position;
                        spawnPos += new Vector3 (0, 1.2f, 0);
                        Instantiate (bulletPrefab, spawnPos, Quaternion.identity);

                        elapsedTime = 0f;
                    }
                }
            }
                }

        void OnTriggerEnter2D(Collider2D other)
        {
                gameController.PlayerDied ();
        }

    }

shipControl我不认为这段代码与问题有任何关系,但万一我发布了它,下一个代码就是问题似乎发生的地方(?),我不确定因为它调用了来自另一个类的方法。问题是第一部分执行并且子弹击中的敌人消失,然而,分数没有更新,子弹也不会消失。

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

public class BulletCode : MonoBehaviour {
    public float speed = 10f;
    private GameControl gameController;

    void Start()
    {
        gameController = GameObject.FindObjectOfType<GameControl> ();
    }

    void Update()
    {
        transform.Translate (0f, speed * Time.deltaTime, 0f);
    }
    void OnCollisionEnter2D(Collision2D other)
    {
        if (other.gameObject.tag == "Meteor_0(Clone)") {
            Destroy (other.gameObject);
            Destroy (gameObject);
        }
    }
}

问题出现在OnCollisionEnter2D方法中,第一个命令执行,但在此之后似乎没有任何事情发生,GameControl代码是

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

public class GameControl : MonoBehaviour {
    public Text scoreText, gameOverText;
    int playerScore = 0;

    public void AddScore()
    {
        playerScore++;
        scoreText.text = playerScore.ToString ();
    }
    public void PlayerDied()
    {
        gameOverText.enabled = true;
        NewGameBtn ();
    }
    void NewGameBtn()
    {
        SceneManager.LoadScene (4);
    }
}

任何有用的东西,我感谢你们这样阅读并帮助我,感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

没有调用AddScore,这解释了为什么分数没有更新 你应该尝试添加它:

if (other.gameObject.tag == "Meteor_0(Clone)") {
    gameController.AddScore();
    Destroy (other.gameObject);
    Destroy (gameObject);
}

现在关于未被销毁的子弹:

  • 请确保不要混合“tag”和“name”(标签在标签字段中设置,name是场景中对象的名称)。 Meteor_0(克隆)似乎是一个名字

  • 如果流星消失,似乎你的代码进入了if块。您仍然可以在Debug.Log("destroying bullet now")

  • 之前通过添加调试行Destroy (gameObject);来检查它
  • 您确定子弹的所有视觉效果都在具有BulletCode脚本的游戏对象上吗?在调用Destroy (gameObject);时检查哪个对象是破坏性的,也许它不是你想要的那个