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 Digital-Phantom · Jan 11, 2015 at 06:21 AM · javascripterror messagenullreferenceexceptioncomponentobject reference

Why am I getting this error ?

I have a line of code in my script to access and enable/disable a certain component on a game object...

 var freezePlayer = GameObject.FindGameObjectWithTag("Player").GetComponent("CharacterMotor");

Works fine.

When I change it to this...

 var freezePlayer = GameObject.FindGameObjectWithTag("Player").GetComponent("MainCamera");

I get this error message...

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

Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)

Any suggestions please guys ???

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 jeffreyrampineda · Jan 11, 2015 at 07:14 AM 0
Share

Is $$anonymous$$ainCamera a script? Try doing .GetComponent($$anonymous$$ainCamera);

avatar image Digital-Phantom · Jan 11, 2015 at 07:16 AM 0
Share

No, sorry possibly didn't explain that too well. Its the actual $$anonymous$$ainCamera that is attached to the first person controller

2 Replies

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

Answer by tanoshimi · Jan 11, 2015 at 08:01 AM

Try

 var freezePlayer = GameObject.FindGameObjectWithTag("Player").GetComponent(Camera);
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 Digital-Phantom · Jan 11, 2015 at 08:32 AM 0
Share

Already tried that. Got this error message -

$$anonymous$$issingComponentException: There is no 'Camera' attached to the "First Person Controller" game object, but a script is trying to access it. You probably need to add a Camera to the game object "First Person Controller". Or your script needs to check if the component is attached before using it. (wrapper dynamic-method) UnityEngine.Camera.Camera$set_enabled$System.Boolean (object,object[]) Boo.Lang.Runtime.RuntimeServices.SetProperty (object,string,object) SwitchCameras.Update () (at Assets/Scripts/SwitchCameras.js:21)

:(

avatar image tanoshimi · Jan 11, 2015 at 08:38 AM 0
Share

Well, that error is pretty self-explanatory... it means there is no 'Camera' attached to the "First Person Controller" game object, but a script is trying to access it. You probably need to add a Camera to the game object "First Person Controller".

avatar image Digital-Phantom · Jan 11, 2015 at 08:47 AM 0
Share

The unity first person controller has a camera 'built in' by default.

(second component in list, $$anonymous$$ain Camera with Tag - $$anonymous$$ainCamera)

avatar image tanoshimi · Jan 11, 2015 at 08:49 AM 1
Share

That's not a component on the same object tagged with "Player" though - that's a component on a child object. Use

  var freezePlayer = GameObject.FindGameObjectWithTag("Player").GetComponentInChildren(Camera);
avatar image
1

Answer by ElDo · Jan 11, 2015 at 08:46 AM

Since the Camera is not a Component i don't think you can get your reference to the Camera with GetComponent. You could either use a Tag and get the Camera with GameObject.FindGameObjectWithTag("MainCamera") or you could get it by Name for example:

 GameObject.FindGameObjectWithTag("Player").transform.FindChild("MainCamera")

or even with:

 Camera.main

you would have to check the js Versions of the above code since i do only use c# i' not sure if the Syntax is the same.

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

27 People are following this question.

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

Related Questions

LOD Error on Unity's own Example script 1 Answer

"NullReferenceException" Error when trying to count objects with a particular tag using GameObject.FindGameObjectsWithTag function 1 Answer

Another Null Reference Exception 1 Answer

Help with eval() 0 Answers

LitJson not working 0 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