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 Matt2013 · Oct 04, 2013 at 01:47 AM · getcomponentexceptionnull reference exceptionaccessing scripts

GetComponent null reference C#

I have 2 scripts attached to a cube I'm trying to get variables from the other script,but it always comes up null. in the start function of the script , physics, I have

     void Start () {
         script = transform.parent.GetComponent<Movement>();
         
     }

The MonoScript editor suggests Class Movement , but when I run it I get a null reference exception. Then I can not use any of Movement's variables.

oops forgot to mention that this was in C#.

twisted's suggestion let's me access the variables in the editor, but when I actually run Unity it still throws an error.

Thanks thought I had use parent , when I didn't.

This worked

     Movement script;
     
     // Use this for initialization
     void Start () {
         script = GetComponent<Movement>() as Movement;
     }
Comment
Add comment · Show 4
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 tw1st3d · Oct 04, 2013 at 01:49 AM 0
Share
 $$anonymous$$ovement script = ($$anonymous$$ovement) transform.parent.GetComponent<$$anonymous$$ovement>() as $$anonymous$$ovement;

ehh, try this.

avatar image ZouBi27 · Oct 04, 2013 at 01:56 AM 0
Share

The two scripts seem to be attached to the same component. So why do you try to get the component from the parent?

Try this:

 script = GetComponent<$$anonymous$$ovement>();
avatar image Bunny83 · Oct 04, 2013 at 03:01 AM 1
Share

@tw1st3d. Your line doesn't make much sense. Why do you cast the result of GetComponent 3 times? (as-cast inside the generic function, explicit as cast and explicit c-style cast). One time is more than enough.

I bet your if-statements look like this:

     if (A==1 && 1==A && A==1)

Just to be sure A is 1 :P

avatar image rutter · Oct 04, 2013 at 03:20 AM 0
Share

@Bunny83: Typecast it from orbit. It's the only way to be sure.

1 Reply

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

Answer by Rahazan · Oct 04, 2013 at 01:57 AM

 void Start() {
    script = GetComponent<Movement>();
 }

The Movement script is probably attached to the same GameObject as the Physics script, and not the parent GameObject (higher in the hierarchy).

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

18 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

Related Questions

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

Can't Call function in another script 1 Answer

How am I supposed to use GetComponent? 1 Answer

Neater way to test an Enum in a different script? 1 Answer

An issue with GetComponent 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