2016-12-13 13 views
0

私はC#を使用して1つのゲームで作業しています。クローンを作成して削除しようとしています。だから私が掲示したコードは、プレイヤーを再スポーンし、彼がrespawnsときに飛び火している。これは火花のクローンを作ります。スパークを削除するのに問題があります。私は自分のコードが間違っているかを知る必要があり、なぜそれがあるタイプをタイプCに変換できません。#

.....経由

をunityengine.gameobjectするタイプのunityengine.transformを変換することはできません。私は、エラーメッセージが取得しますこれを行う。ので、ここで

は、全体のコード

using UnityEngine; 
using System.Collections; 

public class GameMaster : MonoBehaviour { 

public static GameMaster gm; 

void Start() { 
    if (gm == null) { 
     gm = GameObject.FindGameObjectWithTag ("GM").GetComponent<GameMaster>(); 
    } 
} 

public Transform playerPrefab; 
public Transform spawnPoint; 
public float spawnDelay = 2; 
public Transform spawnPrefab; 

public IEnumerator RespawnPlayer() { 
    //audio.Play(); 
    yield return new WaitForSeconds (spawnDelay); 

    Instantiate (playerPrefab, spawnPoint.position, spawnPoint.rotation); 
    GameObject clone = Instantiate (spawnPrefab, spawnPoint.position, spawnPoint.rotation) as GameObject; 
    Destroy (clone, 3f); 
} 

public static void KillPlayer (Player player) { 
    Destroy (player.gameObject); 
    gm.StartCoroutine (gm.RespawnPlayer()); 
} 

} 

であり、ここであなたがpublic Transform spawnPrefab;をしたとき、あなたのプレハブがTransformとして宣言されているので、あなたがエラーを取得する

GameObject clone = Instantiate (spawnPrefab, spawnPoint.position, spawnPoint.rotation) as GameObject; 

答えて

3

にめちゃくちゃにされたラインであります。したがって、GameObjectではなくTransformとしてインスタンス化しています。

ちょうどそれがあなたの破壊ラインでgameObjectだ破壊、単にtransformとしてインスタンス化しても大丈夫です

public GameObject spawnPrefab; 
1

public Transform spawnPrefab; 

を変更、それを修正するには:

Transform clone = Instantiate(spawnPrefab, spawnPoint.position, spawnPoint.rotation) as Transform; 
Destroy(clone.gameObject, 3f); 
関連する問題