2015-01-17 32 views
0

所以我有一個角色能夠拍攝,但是當角色死亡並重新拍攝時他無法拍攝並且出現錯誤: UnassignedReferenceException:武器的變量BulletTrailPrefab尚未分配。我的拍攝腳本在重生後不起作用

您可能需要在檢查器中分配武器腳本的BulletTrailPrefab變量。

UnityEngine.Object.Internal_InstantiateSingle(UnityEngine.Object數據,POS的Vector3,四元數腐)(在C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/UnityEngineObject.cs:74)

UnityEngine.Object。實例化(UnityEngine.Object原,的Vector3位置,四元數旋轉)(在C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/UnityEngineObject.cs:84)

Weapon.Effect()(在資產/ Weapon.cs :64)

Weapon.Shoot()(at Assets/Weapon.cs:53)

Weapon.Update()(在Assets/Weapon.cs:33)

所以,如果有人可以幫助我解決這個問題,將不勝感激。我曾嘗試很多東西但是沒有出現使用UnityEngine工作:(

我的武器腳本

; System.Collections中使用;

公共類武器:MonoBehaviour {

public float fireRate = 0; 
public float Damage = 10; 
public LayerMask whatToHit; 

public Transform target; 

public Transform BulletTrailPrefab; 
float timeToSpawnEffect = 0; 
public float effectSpawnRate = 10; 

float timeToFire = 0; 
Transform firePoint; 

float nextTimeToSearch = 0; 

// Use this for initialization 
void Awake() { 
    firePoint = transform.FindChild ("firePoint"); 
    if (firePoint == null) { 
     Debug.LogError ("No firePoint? WHAT?!"); 
    } 
} 

// Update is called once per frame 
void Update() { 
    if (fireRate == 0) { 
     if (Input.GetButtonDown ("Fire1")) { 
      Shoot(); 
     } 
    } 
    else { 
     if (Input.GetButton ("Fire1") && Time.time > timeToFire) { 
      timeToFire = Time.time + 1/fireRate; 
      Shoot(); 
     } 
    } 
    if (target == null) { 
     FindBulletTrailPrefab(); 
     return; 
    } 
} 

void Shoot() { 
    Vector2 mousePosition = new Vector2 (Camera.main.ScreenToWorldPoint (Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y); 
    Vector2 firePointPosition = new Vector2 (firePoint.position.x, firePoint.position.y); 
    RaycastHit2D hit = Physics2D.Raycast (firePointPosition, mousePosition-firePointPosition, 100, whatToHit); 
    if (Time.time >= timeToSpawnEffect) { 
     Effect(); 
     timeToSpawnEffect = Time.time + 1/effectSpawnRate; 
    } 
    Debug.DrawLine (firePointPosition, (mousePosition-firePointPosition)*100, Color.cyan); 
    if (hit.collider != null) { 
     Debug.DrawLine (firePointPosition, hit.point, Color.red); 
     Debug.Log ("We hit " + hit.collider.name + " and did " + Damage + " damage."); 
    } 
} 

void Effect() { 
    Instantiate (BulletTrailPrefab, firePoint.position, firePoint.rotation); 
} 
void FindBulletTrailPrefab() { 
    if (nextTimeToSearch <= Time.time) { 
     GameObject searchResult = GameObject.FindGameObjectWithTag ("BulletTrail"); 
     if (searchResult != null) 
      target = searchResult.transform; 
     nextTimeToSearch = Time.time + 0.5f; 
    } 
} 

}

我的遊戲管理員腳本

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 int spawnDelay = 2; 

    public IEnumerator RespawnPlayer() { 
     Debug.Log ("TODO: Add waiting for spawn sound"); 
     yield return new WaitForSeconds (spawnDelay); 

     Instantiate (playerPrefab, spawnPoint.position, spawnPoint.rotation); 
     Debug.Log ("TODO: Add Spawn Particles"); 
    } 

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

我的播放器腳本

using UnityEngine; 
using System.Collections; 

public class Player : MonoBehaviour { 

    [System.Serializable] 
    public class PlayerStats { 
     public int Health = 100; 
    } 

    public PlayerStats playerStats = new PlayerStats(); 

    public int fallBoundary = -20; 

    void Update() { 
     if (transform.position.y <= fallBoundary) 
      DamagePlayer (9999999); 
    } 

    public void DamagePlayer (int damage) { 
     playerStats.Health -= damage; 
     if (playerStats.Health <= 0) { 
      GameMaster.KillPlayer(this); 
     } 
    } 
} 

回答

0

的錯誤是很清楚的。您還沒有爲您的變量指定預製件。

UnassignedReferenceException: The variable BulletTrailPrefab of Weapon has not been assigned.

You probably need to assign the BulletTrailPrefab variable of the Weapon script in the inspector.

+0

但我不太清楚如何正確地做到這一點?任何幫助? –

+0

你只需將你的子彈蹤跡預製件拖放到統一體內的檢查器中的腳本變量中即可。看起來你沒有顯示整個腳本,因爲錯誤指向33到74的行,但是如果你嘗試實例化一個遊戲對象,你首先需要某種「模板」並且有一個預製(理想地)分配給它),所以它知道你想要創建什麼。 – walther

+0

以及我已經有bullettrailprefab分配,如你所說,但錯誤仍在這裏。任何其他建議? –

0

我找到了答案。它只是現在的作品:/不知道爲什麼,如何,但它的工作XD

0

回答爲時已晚! (可愛!)

它只發生後你重生,是因爲當你實例化一個新playerPrefab究其原因,Weapon對象(這大概是你的播放器的一部分預製)沒有你已經在做的任務編輯器 - 這些分配僅適用於編輯器中的特定實例。你需要在運行時手動分配它的兩種方法之一:

1)當你重生:

public IEnumerator RespawnPlayer() { 
    // ... 
    GameObject playerInstance = Instantiate (
     playerPrefab, 
     spawnPoint.position, 
     spawnPoint.rotation) as GameObject; 
    Weapon weaponInstance = playerInstance.GetComponentInChildren<Weapon>; 
    weaponInstance.BulletTrailPrefab = /* prefab reference */; 
    // ... 
} 

2)在您的Weapon腳本:

void Start() { 
    BulletTrailPrefab = /* prefab reference */; 
} 

但是在這兩種如果您需要在運行時參考預製件的某種方式,例如putting a reference into the GameMaster script or using Resources.Load()