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 /
This question was closed Sep 14, 2015 at 04:36 PM by greatjulius for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by greatjulius · Sep 12, 2015 at 07:16 PM · 2derrornullreferenceexceptionphysics2dscript error

NullReferenceException, Script Error

Hi,

I wrote a script that should manage that a bool in the animater turns true or false if a BoxCollider2D from the Player called "groundCheck" got triggered by the ground or not. After I finished it and added the script it doesn't worked as I wanted to. An error says:"NullReferenceExeption: Object refernce not set to an instance of an object [...]" I don't know how to fox it. The error seems to apear in line 16 and 21. There is another post in this forum which tells how to fix it but I don't understand it.

 using UnityEngine;
 using System.Collections;
 
 public class GroundCheck : MonoBehaviour {
 
     private Player player;
 
     void start()
     {
         player = gameObject.GetComponentsInParent<Player>()[0];
     }
 
 
     void OnTriggerEnter2D(Collider2D col) 
     {
         player.grounded = true;
     }
 
     void OnTriggerExit2D(Collider2D col)
     {
         player.grounded = false;
     }
 
 }
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 Graham-Dunnett ♦♦ · Sep 12, 2015 at 07:16 PM 0
Share

Take a look at some of the other questions and answers tagged with nullreferenceexception.

avatar image greatjulius · Sep 12, 2015 at 07:56 PM 0
Share

I tryd the "Try/Catch" $$anonymous$$ethod but it doesn't fixed the problem, here is the code: using UnityEngine; using System.Collections;

 public class GroundCheck : $$anonymous$$onoBehaviour {
 
     private Player player;
 
     void start()
     {
         player = gameObject.GetComponentsInParent<Player>()[0];
     }
 
 
     void OnTriggerEnter2D(Collider2D col) 
     {
         try {
             player.grounded = true;
         }
         catch (NullReferenceException ex) {
             Debug.Log("player.grounded was not set in the inspector")
         }
     }
 
     void OnTriggerExit2D(Collider2D col)
     {
         player.grounded = false;
     }
 
 }

I also tryd the "Null check" but also it doesn't helped: using UnityEngine; using System.Collections;

 public class GroundCheck : $$anonymous$$onoBehaviour {
 
     private Player player;
 
     void start()
     {
         player = gameObject.GetComponentsInParent<Player>()[0];
     }
 
 
     void OnTriggerEnter2D(Collider2D col) 
     {
         player.grounded = true;
         if (player.grounded) {
             Debug.Log(player.grounded.name);
         } else {
             Debug.Log("")
     }
 
     void OnTriggerExit2D(Collider2D col)
     {
         player.grounded = false;
     }
 
 }

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by gjf · Sep 12, 2015 at 07:50 PM

player is never getting initialized because the place that you think you're setting it - start() - never gets called.

change it to Start() and it should fix your problem ;)

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 greatjulius · Sep 12, 2015 at 08:16 PM 0
Share

Ok, it solved the NullReferenceExcepttion but the bool of the animator still won't turn true if the player is grounded.

Follow this Question

Answers Answers and Comments

31 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

Related Questions

Why do I keep getting this error? 1 Answer

Referencing another script error/problem 0 Answers

2D Top-Down Shooting Script Errors 1 Answer

Unity Overlap Point Causing Error 0 Answers

How do I change the image of an SVG sprite? 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