2015-12-30 75 views
0

我已經實現用youtube例如一個腳本。但是當我在其他腳本中使用Resource方法時,編譯器會給出一個空引用錯誤。我究竟做錯了什麼。資源不會在運行時統一加載到我的場景中5?

這是幫助我的腳本和資源的調用方式。

using UnityEngine; 
using System.Collections; 

namespace Helper 
{ 
public class Resource 
    { 
     public static string AnimatorController = "System/PlayerAnimator"; 
    } 
} 

根據我現場的道路是

資產/資源/系統/ PlayerAnimator.controller

這是我在其他腳本如何用來加載資源

 private Animator _animator; 
    private RuntimeAnimatorController _animatorController; 

    _animatorController = Resources.Load(Resource.AnimatorController) as RuntimeAnimatorController; 
    _animator.runtimeAnimatorController = _animatorController; 

,這是什麼修復?

我加入這兩個腳本。非沒有工作

PlayerCharctor.cs

using UnityEngine; using System.Collections; using Helper; 


[RequireComponent(typeof(NetworkView))] [RequireComponent(typeof(CharacterController))] [RequireComponent(typeof(Animator))] 

[RequireComponent(typeof(PlayerMotor))] [RequireComponent(typeof(PlayerCamera))] 

[AddComponentMenu("Scripts/Player/PlayerCharacter")] public class PlayerCharacter : MonoBehaviour { 

    #region Variables & Properties (Private) 

    private CharacterController _controller; 
    private Animator _animator; 
    private RuntimeAnimatorController _animatorController; 



    #endregion 

    #region Variables & Properties (Public) 
    public CharacterController Controller 
    { 
     get 
     { 
      return _controller; 
     } 


    } 

    public Animator Animator 
    { 
     get 
     { 
      return _animator; 
     } 


    } 

    #endregion 

    #region Unity Event Funtions 

    void awake() 
    { 
     _animator = GetComponent<Animator>(); 
     _controller = GetComponent<CharacterController>(); 
    } 

    // Use this for initialization 
    void Start() 
    { 

      //Ensure networkview Component exists 
      if (GetComponent<NetworkView>() != null) 
      { 
       //Ensure that initialization only executes if this is a valid instance 
       if (GetComponent<NetworkView>().isMine || Network.peerType == NetworkPeerType.Disconnected) 
       { 
       //Load in the AnimatorController at runtime 
       _animatorController = Resources.Load(Resource.AnimatorController) as RuntimeAnimatorController; 
       _animator.runtimeAnimatorController = _animatorController; 







       _controller.center = new Vector3(0f, 1f, 0f); 
        _controller.height = 1.8f; 
       } 
       else 
       { 
        enabled = false; 
       } 
      } 
      else 
      { 
       Debug.Log("Attach a NetWorkViewComponent!!"); 
      } 




    } 

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

      } 

#endregion 

#region Methods 

#endregion Methods } 

和輔助腳本

using UnityEngine; 
using System.Collections; 

namespace Helper 
{ 

    #region Referance Cache 

    public class PlayerInput 
    { 
     public static string Horizontal = "Horizontal"; 
     public static string Vertical = "vertical"; 

     public static string Jump = "Jump"; 

     public static string RightX = "Mouse X"; 
     public static string RightY = "Mouse Y"; 
    } 

    public class GameTag 
    { 
     //System tags 
     public static string Untagged = "Untagged"; 
     public static string Respawn = "Respawn"; 
     public static string Finish = "Finish"; 
     public static string EditorOnly = "EditorOnly"; 
     public static string MainCamera = "MainCamera"; 
     public static string Player = "Player"; 
     public static string GameController = "GameController"; 

     public static string PlayerCamera = "PlayerCamera"; 
    } 

    public class Resource 
    { 
     public static string AnimatorController= "System/PLController"; //Changed the name of the controller (new one) 
    } 

    public static class AnimatorConditions 
    { 
     public static string Speed = "Speed"; 
     public static string Direction = "Direction"; 
     public static string Grounded = "Grounded"; 
     public static string AirVelocity = "AirVelocity"; 
    } 

    #endregion 

    #region FSM Enumerations (Finite State Machine) 

    public enum CameraState 
    { 
     Normal, 
     Target 
    } 

    public enum SpeedState 
    { 
     Walk, 
     Run, 
     Sprint 
    } 
    #endregion 

    #region Object Structures 

    public struct CameraTargetObject 
    { 
     private Vector3 position; 

     private Transform xForm; 

     public Vector3 Position 
     { 
      get 
      { 
       return position; 
      } 

      set 
      { 
       position = value; 
      } 
     } 

     public Transform XForm 
     { 
      get 
      { 
       return xForm; 
      } 

      set 
      { 
       xForm = value; 
      } 
     } 

     public void Init(string camName, Vector3 pos, Transform transform, Transform parent) 
     { 
      position = pos; 
      xForm = transform; 
      xForm.name = camName; 
      xForm.parent = parent; 
      xForm.localPosition = Vector3.zero; 
      xForm.localPosition = position; 
     } 
    } 

    public struct CameraMountPoint 
    { 
     private Vector3 position; 

     private Transform xForm; 

     public Vector3 Position 
     { 
      get 
      { 
       return position; 
      } 

      set 
      { 
       position = value; 
      } 
     } 

     public Transform XForm 
     { 
      get 
      { 
       return xForm; 
      } 

      set 
      { 
       xForm = value; 
      } 
     } 

     public void Init(string camName, Vector3 pos, Transform transform, Transform parent) 
     { 
      position = pos; 
      xForm = transform; 
      xForm.name = camName; 
      xForm.parent = parent; 
      xForm.localPosition = Vector3.zero; 
      xForm.localPosition = position; 
     } 
    } 

    #endregion 


} 

回答

0

。在你從Resources裝載資產的代碼沒有問題。但實際上可能是因爲你的_animator變量。我99%確定它是null。首先得到像_animator = GetComponent<Animator>();Animator組件然後指定其RuntimeAnimatorController

+0

好,我會試着讓你知道。大概你是對的。 –

+0

我已添加獲取組件。仍然沒有工作。 –

+0

確保你已經從檢查員 –

相關問題