Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by oliver82957 · May 09, 2017 at 06:14 AM · c#errorgetcomponentstandard assets

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.

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Piyush_Pandey · May 09, 2017 at 07:00 AM 0
Share

Please share code samples.

avatar image Namey5 · May 09, 2017 at 07:03 AM 0
Share

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.

avatar image oliver82957 · May 09, 2017 at 01:56 PM 0
Share

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.

avatar image oliver82957 · May 09, 2017 at 02:03 PM 0
Share

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.

avatar image oliver82957 · May 10, 2017 at 12:09 AM 0
Share

Thank You for the help. It now works out well apart from the animation looping itself after activation but I can solve that.@Bunny83

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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;
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Null Reference Exception Help 1 Answer

Distribute terrain in zones 3 Answers

C# getComponent result error cast 3 Answers

GetComponent with Standard Assets? C# 1 Answer

Problem calling variable across scripts (Closed) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges