0

YouTubeの例を使用してヘルパー スクリプトを実装しました。しかし、他のスクリプトで Resource メソッドを使用すると、コンパイラは null 参照エラーを返します。私は何を間違っていますか。

これは私のヘルパー スクリプトであり、リソースの呼び出し方法です。

using UnityEngine;
 using System.Collections;

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

私のシーンによるパスは

Assets/Resources/System/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 }

AND ヘルパー スクリプト

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


}
4

1 に答える 1