2017-11-05 7 views
0

プレイヤーを破壊しようとしています。自分のプラットフォームにスクリプトを添付して、プレイヤーと衝突してタイマーを開始し、タイマーが終了したら、gameオブジェクト(プラットフォーム自体)を破棄するようにしました。私もそれを持っていたいので、プレイヤーがなくなったときにプレイヤーがプラットフォームに乗ってしまった場合、プレーヤーも破壊されます。だから私はブールを作りましたが、ブールが真ならば、ただプレーヤーを破壊するのは難しいです。Unityでゲームオブジェクトを破壊する方法

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

public class DestroyAddPlatform : MonoBehaviour { 

    //This will be used to set the seconds 
    public float timeLeft = 3f; 
    bool playerOn = false; 
    private float PlayerGameObject; 


    // Use this for initialization 
    void Start() { 

    } 

    // Update is called once per frame 
    void Update() { 

    } 

    //How you declare a Coroutine Function. 
    IEnumerator destroyer(){ 
     //wait for the timeLeft in seconds. 
     yield return new WaitForSeconds (timeLeft); 

     //Then destroy the object this script is attached to 
     Destroy (gameObject); 
    } 

    //A timer that will start the coroutine 
    void destroyTimer(){ 
     //coroutine calls the destroyer function, its a function that runs independently. 
     StartCoroutine ("destroyer"); 

     //At this point tell it to add in a new platform. 
    } 

    //This function will be in accordance to a collision. 
    void OnCollisionEnter2D (Collision2D other){ 

     if (other.gameObject.CompareTag ("Player")) 
      //If the object is the player, run 
      destroyTimer();//Start the destroy timer funciton. 
    } 

}

+0

はあなたがしようとしたコードを追加することができれば、タイマーが実行されたときに

PlayerGameObject = null; 

が次にチェック設定されていますか?このポストへ –

+0

あなたに私たちに特定の問題とそれを引き起こすコードを与えていないなら、私たちはあなたを助けることはできません。 'unityscript'タグも関連していると確信していますか?あなたが本当にそれを使用しているなら、私は切り替えを提案しています。もしあなたが既にC#を使用しているなら、タグを削除してください:-)。 – George

+0

私は自分のコードを掲載し、unityscriptタグを削除しました。 –

答えて

0

あなたはdestroy方法でそれを行うことができます。単にスクリプトを作成してオブジェクトに添付します(ゲームでは決して破壊されません)。次に、そのスクリプトにプレーヤーオブジェクトを割り当て、destroyメソッドを使用します。

public var Player : GameObject; 
//call this method at desired event 
    function DestroyPlayer(){ 
     Destroy(Player); 
    } 
0

OnCollisionEnterプレーヤーでは、プレーヤーのgameobjectを変数PlayerGameObjectに格納します。プレーヤー

OnCollisionExitは

if(PlayerGameObject != null) 
{ 
    Destroy(PlayerGameObject); 
} 
関連する問題