Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by roaringdoom · Sep 21, 2014 at 12:31 AM · nullreferenceexceptiongetcomponentunderwatercharactermotor

NullReferenceException-Error when trying to GetComponent

Hi guys!

I got multiple planes (lakes) in my scene and if my player moves to a lower y-value than the plane-positions, an underwater-look is activated!

Every single lake has a script attached to it and calls a function in the script "Overseer" (which is located on a "manager-object"), whenever the player starts diving... Now the problem is, that when the Overseer-script tries to get the component "CharacterMotor" on the main camera to deactivate it, it doesn't work.

In fact, Unity gives me a NullReferenceException-Error. It seems like my script is not capable to/doesn't find the "CharacterMotor"-script!?

Anyway, here are the lines, on which it gives me the error:

 ...
 var normalmotor : CharacterMotor = FirstPersonController.GetComponent.<CharacterMotor>();
   if(normalmotor.enabled == false){
       normalmotor.enabled = true;
       }
 ...

I really don't know what I did wrong so I'm grateful for any kind of help!

Thanks

Comment
Add comment · Show 2
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 hatake3 · Sep 21, 2014 at 01:24 AM 0
Share

Does the FirstPersonController object have the Character$$anonymous$$otor script on it?

avatar image roaringdoom · Sep 21, 2014 at 04:47 PM 0
Share

Yes. Actually it has an activated Character$$anonymous$$otor-script and an unactive copy of the "Character$$anonymous$$otor-script", called Character$$anonymous$$otorUnderwater on it.

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by roaringdoom · Oct 06, 2014 at 01:17 PM

Thank you all for your answers!

I solved the problem by just adding the CharacterController-, the CharacterMotor- and the FSPInputController-Components/Scripts in the Start Function of my "Overseer" and saving it as a variable:

 function Start (){
     charactercontroller = FirstPersonController.AddComponent(CharacterController);
     charactermotor = FirstPersonController.AddComponent(CharacterMotor);
     FirstPersonController.AddComponent(FPSInputController);
 }

Like that, I was able to disable and enable the different scripts/components:

 ...
 charactercontroller.enabled = false;
 charactermotor.enabled = false;
 ...
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
avatar image
0

Answer by Slev · Sep 21, 2014 at 05:02 PM

You have a typo. It should be:

 var normalmotor : CharacterMotor = FirstPersonController.GetComponent<CharacterMotor>();

You had an extra period between the function and the type directive.

Comment
Add comment · Show 4 · 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
avatar image roaringdoom · Sep 21, 2014 at 06:17 PM 0
Share

Are you sure? Because then it gives me three new errors... Even if I write

var normalmotor : Character$$anonymous$$otor = FirstPersonController.GetComponent(Character$$anonymous$$otor);

it gives me the same NullReferenceException-Error!

avatar image Louis Watson · Sep 21, 2014 at 06:51 PM 0
Share

just throwing it out there but FirstPersonController. ... seems wrong you need an instance of the class to use GetComponent ie: var firstPersonController : FirstPersonController = FindObjectOfType; or the like

avatar image roaringdoom · Sep 25, 2014 at 09:08 PM 0
Share

Thank you for your answer! I already defined the variable "FirstPersonController" as a GameObject earlier in the script. So that shouldn't be a problem, right?

avatar image roaringdoom · Oct 04, 2014 at 08:50 PM 0
Share

I'm sorry if I wasn't clear enough, but I'm using JavaScript!

avatar image
0

Answer by cflowers · Oct 04, 2014 at 10:00 PM

You might know some of the things I am about to say but for others sake I am going to state them. First you need a script called exactly "CharacterMotor" for this is case-sensitive. Then one must put the "CharacterMotor" script on the FirstPersonController object. Then one way to call the script is:

var normalmotor:CharacterMotor = (CharacterMotor)FirstPersonController.GetComponent("CharacterMotor");

I did something similar like this, this is in C#:

 public GameObject thisObject;//could be any object I want
 Monologue mono1 = (Monologue)thisObject.GetComponent("Monologue");//the object I pick must have this script on it or it will be null





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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

NullReferenceException: Object reference not set to an instance of an object 1 Answer

GetComponent for siblings and parents 2 Answers

Why is this null? Finding a script on an object 3 Answers

Underwater scripts 2 Answers


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