重生后,我的拍摄脚本不起作用

时间:2015-01-17 13:03:22

标签: c#

所以我有一个能够射击的角色然而当角色死亡并且重生时他无法射击并且我得到错误: UnassignedReferenceException:尚未分配武器的变量BulletTrailPrefab。

您可能需要在检查器中分配武器脚本的BulletTrailPrefab变量。

UnityEngine.Object.Internal_InstantiateSingle(UnityEngine.Object data,Vector3 pos,Quaternion rot)(at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/UnityEngineObject.cs:74)

UnityEngine.Object.Instantiate(UnityEngine.Object original,Vector3 position,Quaternion rotation)(at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/UnityEngineObject.cs:84)

Weapon.Effect()(在Assets / Weapon.cs:64)

Weapon.Shoot()(在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);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

错误很明显。您尚未为变量指定预制件..

  

UnassignedReferenceException:武器的变量BulletTrailPrefab   尚未分配。

     

你可能需要分配一个BulletTrailPrefab变量   检查员中的武器脚本。

答案 1 :(得分: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()