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 /
  • Help Room /
avatar image
0
Question by Cloudly · Jun 16, 2016 at 01:09 PM · error messagenull reference exception

Object reference not set to an instance of an object ERROR - Idk why

Whats the correct way it should be written?

 using UnityEngine;
 using System.Collections;
 
 public class GroundCheck : MonoBehaviour {
 
     private Player player;
 
     void Start () {
 
         player.gameObject.GetComponentsInParent<Player> ();
     
     }
 
     void OnTriggerEnter2D(Collider2D col){
 
         if (null == player) {
             Start ();
         }
 
         player.grounded = true;
     }
 
     void OnTriggerStay2D (Collider2D col){
 
         player.grounded = true;
     }
 
     void OnTriggerExit2D(Collider2D col){
 
         player.grounded = false;
     }
 
 
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Dibbie · Jun 16, 2016 at 03:52 PM

Im noticing a few weird things...

On line 6, you have private Player player, then line 10, you have player.gameObject.GetComponentsInParent<Player>();, but why? You are linking Player (which I assume is NOT another script), and trying to get the component of something that (most likely) doesnt exist on... Itself? What are you trying to look for with line 10 exactly? What component specifically? Unless "Player" is a script, there is no Unity component named "Player".

Line 16, I think your if statement is backwards, it should probably be if(col.transform = player) assuming your "player" is a Transform

You should never call "Start", the purpose of it is to just run only once, ever in your script, being the "entry function" or the very first function thats fired up, executed, and then never messed with again - create another function to do that and call that instead, and since its one line of code, its probably better to just use Line 10 in replacement of Line 17, unless you plan on making it much longer later?

I think the biggest issue is how you created "player" as a reference to "Player", that it just doesnt know what your talking about. What exactly is "Player" and where does it exist?

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 Kionte · Jun 16, 2016 at 04:21 PM 0
Share

@Dibbie can you look at my question I think you would be able to help. I posted it about 40 $$anonymous$$utes ago.

Here is a link. It is also in the null reference exception section. Thanks!

http://answers.unity3d.com/questions/1203688/null-reference-exception-with-custom-class-object.html

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

59 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Null Reference Exception Error 1 Answer

Can someone make sense out of seemingly inconsistent null reference exception? 3 Answers

is my enemy selector null reference a problem? 1 Answer

GameObject only spawning 60% of the time (c#) 1 Answer

ArgumentNullException: Value cannot be null. parameter name: Source 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