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



大家好,所以我知道团结一致,我一直在研究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 ();
        }
    }

我认为此代码与问题无关,但以防万一我发布了下一个代码,问题似乎是出现问题的地方(?(,我不确定,因为它打电话给来自另一类的方法。问题是第一部分执行,敌人被子弹击中消失,但是,得分未更新,子弹也不会消失。

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);
    }
}

任何有帮助的人,感谢你们阅读并帮助我,感谢您的任何帮助。

没有呼叫AddScore,这解释了为什么不更新分数您应该尝试添加它:

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

现在关于子弹未被破坏:

  • 请确保您不混合"标签"one_answers"名称"(在标签字段中设置了标签,名称是场景中对象的名称(。Meteor_0(克隆(似乎是一个名称

  • 如果流星消失,您的代码似乎位于IF块中。您仍然可以在Destroy (gameObject);

  • 之前通过添加DEBUG LINE Debug.Log("destroying bullet now")进行检查
  • 您确定子弹的所有视觉效果都在具有子弹码脚本的游戏对象上吗?在调用Destroy (gameObject);时检查哪个对象是命运,也许不是您想要的

相关内容

  • 没有找到相关文章

最新更新