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 shadow1x1equals2 · Nov 04, 2015 at 11:11 AM · c#childdisableenable

NullReferenceException: Object reference not set to an instance of an object ChildIdentifi.OnTriggerEnter2D (UnityEngine.Collider2D coll) (at Assets/ChildIdentifi.cs:35)

So, hi!

I'm new to unity and to c# programming... And I don't know what is happening.

I have this script:

 using UnityEngine;
 using System.Collections;
 
 public class ChildIdentifi : MonoBehaviour
 {
     bool collide;
     bool Open;
     private Animator anim;
     public string AnimName;
     public string Disable;
     private GameObject other;
     private Component m;
     // Use this for initialization
     void Start ()
     {
         anim = GetComponent<Animator>();
         other = GameObject.Find(Disable);
         m = other.GetComponent<Collider2D> ();
     }
     
     // Update is called once per frame
     void Update ()
     {
         if (Open == true) {
 
             anim.SetTrigger (AnimName);
             Destroy (m);
         }
     }
     
 
     void OnTriggerEnter2D(Collider2D coll) {
         ChildTrigger Child = GetComponent<ChildTrigger> ();
         if (coll) {  
             if (Child.Child == "obj1") {
                 Open = true;
 
             }
         }
     }
 }

And I also have this alert after trying to run it with my game:

NullReferenceException: Object reference not set to an instance of an object ChildIdentifi.OnTriggerEnter2D (UnityEngine.Collider2D coll) (at Assets/ChildIdentifi.cs:35)

When I tried debugging the script, nothing comes out, so everything looks nice... The script is component of a 2D trigger. I'm trying to make a door. If the 'obj1' is the child of the parent 'Player', the collider on a cube that is blocking the way should be destroyed. But if the player is trying to pass by the door without this 'key', it should not be open, and he or she would need to find the child object. It's like a level divisor. If you guys have any idea for the game or to clear out this problem or any more questions about the game or the problem, I accept suggestions.

Thanks in advance, any help counts.

I've searched Google, unity forums and stackoverflow.com, nothing could help me out

Comment
Add comment · Show 5
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 meat5000 ♦ · Nov 04, 2015 at 12:08 PM 0
Share

As well as causing yourself potential problems by using names that could already be taken you must remember that whereas an object can only have 1 parent, they can have multiple children. You should observe the methods used to acquire the children or Components In Children as specified in the Transform and GameObject SAPI pages.

As you use a custom script 'ChildTrigger' which constitutes part of the error, you should post the code for that Class.

avatar image maccabbe · Nov 04, 2015 at 12:31 PM 0
Share

$$anonymous$$ost likely you don't have a ChildTrigger component attached to the same game object as your ChildIdentifi is attached to so GetComponent<ChildTrigger> (); return null (nothing). Then when you try to use Child.Child on line 35 you are trying to get the Child variable of nothing which causes an exception. You can check this using the following at line 33.

 ChildTrigger Child = GetComponent<ChildTrigger> ();
 Debug.Log(Child==null);
avatar image shadow1x1equals2 · Nov 04, 2015 at 01:16 PM 0
Share

@maccabbe, I'm trying to import this variable from another file, which is called ChildTrigger.cs. As I finished reading your comment, I think the problem is in importing this variable, I'm trying to do this at Line 33.

avatar image maccabbe · Nov 04, 2015 at 02:50 PM 0
Share

Trying to import a variable from a c# file doesn't make sense, you are probably trying to get a variable from an object.

GetComponent<ChildTrigger>() is used to get a ChildTrigger attached to the same object. If you do not have a ChildTrigger attached to the same object then you either need to attach a ChildTrigger to the same object or use a different function.

If you are new to C# program$$anonymous$$g then I recommend going through a program$$anonymous$$g tutorial. For instance Unity has a program$$anonymous$$g tutorial at

https://unity3d.com/learn/tutorials/topics/scripting

The module Beginner - 20 covers how to use GetComponent

https://unity3d.com/learn/tutorials/modules/beginner/scripting/getcomponent?playlist=17117

avatar image shadow1x1equals2 · Nov 04, 2015 at 06:28 PM 0
Share

@maccabbe, thanks... I really need this variable... I need to know if the player has a child and I was trying to see it using this object(ChildTrigger). Gonna try something else. Regards.

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Cant Disable Script on Another Gameobject 1 Answer

How to activate an object after "x" seconds (C#)? 4 Answers

(C#) How do I Enable/Disable Visualibility of an GameObject and its Children, Grandchildren, Greatgrandchildren and so on? 1 Answer

enabling item after the other one is disabled and so on? 0 Answers

Pickup number of objects and have another object appear? 2 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