- Home /
Problem when Getting a component from a non Mono Behavior class.
I am sort of new to Unity and I ran into this problem when trying to create an animator script to work with the RigidbodyFirstPersonController script in Unity standard assets.This involves getting booleans from it which I have created. "ArgumentException: GetComponent requires that the requested component 'MovementSettings' derives from MonoBehaviour or Component or is an interface."This is the error message I got when playing the game. Everything else is fine but the animations don't seem to play because of this. This is my first Unity answers so tell me if I should show more or go more in depth. Please tell me if there is an easier way to make my way around it or fixing it would be better.
Wait how exactly are you using GetComponent if the script doesn't derive from a $$anonymous$$onoBehaviour? I understand that you can import classes and namespaces to work from, but you wouldn't be able to attach the script to the object without it being a $$anonymous$$onoBehaviour.
using UnityEngine; using System.Collections; using UnityStandardAssets.Characters.FirstPerson;
public class PlayerAnimatorControllerScript : $$anonymous$$onoBehaviour { Animator animator; void Start () { animator = GetComponent(); }
void Update ()
{
GameObject thePlayer = GameObject.Find("RigidBodyFPSController");
RigidbodyFirstPersonController.$$anonymous$$ovementSettings playerScript = thePlayer.GetComponent<RigidbodyFirstPersonController.$$anonymous$$ovementSettings>();
bool isRunningAnim = playerScript.isRunning;
bool isWalkingAnim = playerScript.isWalking;
animator.SetBool("RunAnim", isRunningAnim);
animator.SetBool("WalkAnim", isWalkingAnim);
There is a lot more animations than just those two but they are the same style. This is my animator controller for the player.
namespace UnityStandardAssets.Characters.FirstPerson { [RequireComponent(typeof (Rigidbody))] [RequireComponent(typeof (CapsuleCollider))] public class RigidbodyFirstPersonController : $$anonymous$$onoBehaviour {
[Serializable]
public class $$anonymous$$ovementSettings
I noticed that the problem was here, the movement settings did not have a $$anonymous$$onoBehaviour. I changed it to a $$anonymous$$onoBehaviour but another error occurred. I created a debug.log that would tell me if it was working like so, if (input.y > 0 && Input.Get$$anonymous$$ey(Run$$anonymous$$ey)) { isRunning = true; Debug.Log("IsRunning"); } else { isRunning = false; } Is running played in the console which meant it was working but I still had that error.
Thank You for the help. It now works out well apart from the animation looping itself after activation but I can solve that.@Bunny83
Answer by Bunny83 · May 09, 2017 at 02:55 PM
GetComponent can only get references to components as the name might suggest. The nested class MovementSetting is a pure data class. The "RigidbodyFirstPersonController" component most likely has a public variable of that type.
It actually looks like this:
public MovementSettings movementSettings = new MovementSettings();
So what you have to do is get the reference to the RigidbodyFirstPersonController component by using GetComponent and just access it's movementSettings variable
var rfpc = thePlayer.GetComponent<RigidbodyFirstPersonController>();
bool isRunningAnim = rfpc.movementSettings.isRunning;
bool isWalkingAnim = rfpc.movementSettings.isWalking;
Your answer
Follow this Question
Related Questions
Null Reference Exception Help 1 Answer
Distribute terrain in zones 3 Answers
C# getComponent result error cast 3 Answers