如何在Unity(C#)中的脚本之间进行通信?

时间:2017-12-03 22:11:41

标签: c# unity3d

我正在团结一起制作炸弹人游戏。一旦爆炸炸弹向炸弹再次提供炸弹,我就会出错。有一个播放器脚本和一个炸弹脚本。当炸弹爆炸时,我想在播放器脚本中增加玩家炸弹计数器,但这给了我一个空引用异常。

public class Bomb : MonoBehaviour {
public AudioClip explosionSound;
public GameObject explosionPrefab; 
public LayerMask levelMask; // This LayerMask makes sure the rays cast to check for free spaces only hits the blocks in the level
private bool exploded = false;
public Player _Player;//link to player
// Use this for initialization
void Start() {
    Invoke("Explode", 3f); //Call Explode in 3 seconds
}

    if (exploded == true)
    {
        if (this.name == "Player 1's bomb(Clone)")
        {
            _Player.bombsP1 += 1;
        }

        else if (this.name == "Player 2's bomb(Clone)")
        {
            _Player.bombsP2 += 1;
        }
    }

1 个答案:

答案 0 :(得分:0)

使用标签:P所以也许它会更容易并将玩家设置为“GameObject”并将其拖放到Inspector中