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
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.
$$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);
@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.
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
@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.