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 j_perker · Mar 12, 2014 at 04:18 PM · null reference

Why am I getting a null reference error?

PlayerHitNetworkScript phns = yourPlayer.GetComponent();

Also I use to get an error from this but my game would still run, but now I'm getting the same error and the game pauses.

Comment
Add comment · Show 6
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 robertbu · Mar 12, 2014 at 04:19 PM 1
Share

If this is the line, then 'yourPlayer' is null. We would need to see the rest of the script to give you any more information on why it is null.

avatar image Graham-Dunnett ♦♦ · Mar 12, 2014 at 04:32 PM 0
Share

or search for one of the million questions about null references.

avatar image j_perker · Mar 15, 2014 at 05:46 PM 0
Share
 GameObject yourPlayer = GameObject.FindGameObjectWithTag("myPlayer");
 PlayerHitNetworkScript phns = yourPlayer.GetComponent<PlayerHitNetworkScript>();

Still getting the null reference error, for some reason unity goes ahead even though it's an error and runs the game normally but other times it doesn't run and pauses. I don't know why it changes but right now it's pausing and isn't going ahead. Not sure why it's saying yourPlayer is null since I'm creating it in the line above.

avatar image j_perker · Mar 15, 2014 at 05:52 PM 0
Share

I'm trying to put it in a try catch block but not really sure how to do it

 try{
             yourPlayer = GameObject.FindGameObjectWithTag("myPlayer");
             phns = yourPlayer.GetComponent<PlayerHitNetworkScript>();
         }
         catch(){
         }


What would I need in the catch parameters? I tried putting "Exception e" but that didn't work.

avatar image Graham-Dunnett ♦♦ · Mar 15, 2014 at 06:42 PM 0
Share

You said "since I'm creating it in the line above". I can see in the line above you are asking Unity to find a game object, but it's possible Unity can't/doesn't find myPlayer. It's worth checking that you get a value assigned to yourPlayer before you try and access a component it may or may not contain. What happened when you did catch(Exception e)?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by spraycanmansam · Mar 15, 2014 at 08:42 PM

From the information you've provided, it's safe to assume that your 'yourPlayer' variable is null. It's always a good idea to assume GetComponent returns null and deal with it then, something like ---

 yourPlayer = GameObject.FindGameObjectWithTag("myPlayer");
 if(yourPlayer != null) {
     // ...
 }
 else {
     Debug.LogError("Could not find 'myPlayer'");
 }

Double check your player and tags.

Comment
Add comment · Show 1 · 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 j_perker · Mar 15, 2014 at 10:59 PM 0
Share

I get the Could not find "myPlayer" error in debug but unity still pauses in the editor. When I run the exe it works fine though.

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

22 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

Related Questions

Mecanim random Bugs 1 Answer

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

Object reference not set to an instance of an object? C# 1 Answer

Creating an array of structs 0 Answers

Scriptable objects has empty inspectors and do not exist in build 4 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