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 /
avatar image
0
Question by The_Unity_Game_Developer · Oct 16, 2015 at 08:53 PM · 2dtrigger2d-platformer

C# issues ; OnTriggerEnter2D

Hello All!

I'm having issues with my 2D game code, more specifically with OnTriggerEnter2D.

I get this error:

 NullReferenceException: Object reference not set to an instance of an object
 KillPlayer.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/Scripts/KillPlayer.cs:21)


Here is my code.

LevelManager.cs:

 using UnityEngine;
 using System.Collections;
 
 public class LevelManager : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     
     }
     
     public void RespawnPlayer () {
         Debug.Log("Player Respawned.");
     }
 }

KillPlayer.cs:

 using UnityEngine;
 using System.Collections;
 
 public class KillPlayer : MonoBehaviour {
 
     public LevelManager levelManager;
 
     // Use this for initialization
     void Start () {
         levelManager = GetComponent<LevelManager>();
     }
     
     // Update is called once per frame
     void Update () {
     
     }
     
     void OnTriggerEnter2D (Collider2D other) {
         
         if(other.name == "Player") {
             levelManager.RespawnPlayer();
         }
         
     }
 }

I have no idea what to do. Any help?

Thanks in advance!

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 NewPath · Oct 16, 2015 at 08:57 PM

A debugger would be the quickest way to figure out what's null, but since the error gives you the exact line number and there's only a single method call on that line, I'm going to take a wild guess that your "levelManager" variable is null.

Comment
Add comment · Show 5 · 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 The_Unity_Game_Developer · Oct 16, 2015 at 09:08 PM 0
Share

How would I go about fixing that? I'm assigning level$$anonymous$$anager to the component in Start()

avatar image NewPath The_Unity_Game_Developer · Oct 16, 2015 at 09:13 PM 0
Share

First, if your intent is to grab it at runtime like that, I would make it private for encapsulation purposes.

I would assume that the GetComponent() call did not find it, and thus returned null. Again, a fairly simple question that you can answer in seconds by simply debugging.

Are you sure that script is actually attached to the object in question and not one of its children?

avatar image The_Unity_Game_Developer · Oct 16, 2015 at 09:19 PM 0
Share

The $$anonymous$$illPlayer script is attatched to a game objet called spikes (set to be a trigger) and the Level$$anonymous$$anager script is attached to an empty game object. Also, what shall I do if level$$anonymous$$anager returns null?

avatar image NewPath The_Unity_Game_Developer · Oct 16, 2015 at 09:27 PM 0
Share

So think about what you are doing. You're calling GetComponent() from an object that you already know does not have a Level$$anonymous$$anager associated with it. Why would that return anything but null.

1) Leave level$$anonymous$$anager public as you have it. 2) Remove the GetComponent() call altogether. 3) In the editor, drag the empty game object onto the level$$anonymous$$anager property on your spikes object. You should see it create a reference to the Level$$anonymous$$anager script.

avatar image The_Unity_Game_Developer · Oct 16, 2015 at 09:33 PM 0
Share

Thank you, I'll try it. I'll mark the answer as accepted once it is tested.

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

39 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

Related Questions

Play 2D animations via trigger parameters 1 Answer

2D Coins and Points 1 Answer

"cover" a Trigger2D using a sprite 0 Answers

Best 2d User Interface for Mobile and Other platforms 1 Answer

Is this normal behavior for edge collider triggers (2D)? 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