1

gameObjectスポーンする「プレーヤー」がありますOnServerInitialized()。の場合はタグ「敵」が「プレイヤー」に変わりGetComponent<NetworkView>().isMineます。

私は次のようなものを作りたいです:

void OnTriggerEnter (Collider Enemy){ 
    if (ScoreManager.score > Enemy.Score) {
            ScoreManager.score = ScoreManager.score + Enemy.Score;
    }
    else if (ScoreManager.score < Enemy.Score) {
Destroy (gameObject);
    }
}

しかし、スポーンした敵プレイヤーのポイントにアクセスする方法がわかりません。

私の ScoreManager スクリプト:

public static int score;

Text text;

void Awake () {
    text = GetComponent <Text> ();
    score = 0;  
}
void Update () {    
    text.text = "Score: " + score;
 }
}

gameObjectという名前の GUI テキストに添付されますScoreText

4

1 に答える 1

0

1) GetComponent を使用しているときは、常に null をチェックします。

2) GetComponent を使用しないでください。[System.Serializable]上記を追加して変数をシリアル化 (または公開)Text text;し、インスペクター (テキスト コンポーネント) にドラッグ アンド ドロップします。

3)フレームごとにスコアを更新する必要はなく、変更された場合にのみ更新します。これは、イベントを使用して行う必要があります。

敵のポイントにアクセスするには、GameObject.FindWithTagを使用し、敵がこのタグを持っている場合は「敵」を使用します。この方法で、敵のゲーム オブジェクトへの参照を取得できます。次に、そのスコア コンポーネントにアクセスします。

例えば:

ScoreManager enemyScoreManager = null;
GameObject enemyReference = GameObject.FindWithTag("Enemy");
if (enemyReference !=null)
    enemyScoreManager = enemyReference.GetComponent<ScoreManager>();
if (enemyScoreManager != null)
{
    enemyScoreManager.score -=5; //steal 5 points
    myScore.score +=5; //assuming myScore is a reference to your score manager
}
else
    Debug.LogError("Enemy not found");
于 2015-06-24T16:36:09.197 に答える