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 sneakyV · Apr 19, 2015 at 04:59 PM · nullreferenceexceptiononcollisionenteroncollisionexit

Object Reference Not Set

I realize this question has been asked many times, but I looked through the other questions and couldn't find my issue. Here's my code:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class DisableMoveWhileFlying : MonoBehaviour {
     private Move obj;
 
     private List<GameObject> Colliding;
 
     void Start(){
         obj = GetComponent<Move>();
     }
     void OnCollisionExit2D(Collision2D Other){
         obj.enabled = false;
         Colliding.Remove(Other.gameObject);
         Debug.Log(Other.gameObject);
     }
     void OnCollisionEnter2D(Collision2D Other){
         obj.enabled = true;
         Colliding.Add(Other.gameObject);
     }
     
     
 }


This gives a Null Reference Exception, and I can't find why. The lines that toggle obj.enabled work fine, but the Debug.Log() line does not.

Thank you.

Comment
Add comment · Show 3
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 hbalint1 · Apr 19, 2015 at 07:19 PM 0
Share

try debugging, or put more Debug.Logs. you should make sure, the Other GameObject gets into the Colliding List. $$anonymous$$aybe you want to Remove it, but it has never been there.

avatar image hangemhigh · Apr 19, 2015 at 07:30 PM 0
Share

What is the name of the Game object the $$anonymous$$ove script is attached to?

avatar image sneakyV · Apr 19, 2015 at 11:44 PM 0
Share

Thank you everyone, I fixed it. private List Colliding does not actually initialize a list. private List Colliding = new List does. Thanks everyone for your time.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by OmniKeith · Apr 19, 2015 at 08:34 PM

you need to disable the object after you call on it. you have the object disabled then when you try to call on the object, it looks for the object and because it is disabled it can't find it.

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

Answer by galshal · Apr 19, 2015 at 08:04 PM

maybe you should try

  void Start(){
          obj = gameObject.GetComponent<Move>();
      }

i had the same problem i think that solved it

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

6 People are following this question.

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

Related Questions

OnCollisionEnter2D and OnCollisionExit2D issue 1 Answer

C#: How can you make your character jump only when you are touching the top of the object? 2 Answers

OnCollisionEnter/Exit and Collider edges. 1 Answer

How to make multiple gameobjects instantiate from one collider? 3 Answers

Maintaining the set of contact points. 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