public class Door : MonoBehaviour {
public Door(Collider2D enterArea, Animation anim){...}
}
public class DoorHandler : MonoBehaviour {
public Door door1 { get ; set; }
...
void Start () {
door1 = new Door(enterArea, openAnimation);
}
}
public class Panel : MonoBehaviour {
GameObject go;
Component comp;
Door thisDoor;
void Start () {
go = GameObject.Find("door");
Debug.Log(go.name); // it gets the right name
doorHandler door = go.GetComponent<doorHandler>();
thisDoor = door.door1;
Debug.Log(thisDoor.name); // Nullpoint ...
}
}
Please would anyone tell me what I am doing wrong? I got nullpointer exception.
Well, first you spelled DoorHandler with an UpperCase D, which is a proper C# naming btw, though you must use the same later here
DoorHandler door = go.GetComponent<DoorHandler>();
Also, Door is a MonoBehaviour, though you create one, but never assign it.
door1 = new Door(enterArea, openAnimation);
You most probably need to assign it
door1 = AddComponent<Door>();
I really don’t get it. The “D” was only typo here. I’ll try to be more precise. I’m just randomly playing with Unity classes and I stucked with this. I am trying to implement auto-doors with a lock. I really want to solve it to learn something. So I’ve got a door class that serves as a blueprint. I’ve removed the MonoBehavior inheritence - I dont need it. Then I have a DoorHandler, that controls my door during the runtime using the methods from Door class. There are simple if statements to check collision with player. An then there’s the Panel that serves as a clickable lock. Trough that I need to access the door instance and its methods. Thanks anyway I appreciate your time. Code should tell better. The DoorHandler script is assigned to an empty scene object called door. And the Panel is assigned to another object called DoorControler.
public class Door {
Collider2D enterArea;
Animation anim;
public bool isOpen;
public bool isLocked;
public Door(Collider2D enterArea, Animation anim) {
this.enterArea = enterArea;
this.anim = anim;
isOpen = false;
isLocked = false;
}
public void lockMe()
{
isLocked = true;
}
public void unlockMe()
{
isLocked = false;
}
public void open()
{
if (!isOpen && !isLocked)
{
anim.Play("open");
}
}
public void close()
{
if (isOpen)
{
anim.Play("close");
}
}
}
public class DoorHandler : MonoBehaviour {
public Door door1 { get ; set; }
Collider2D enterArea;
Animation openAnimation;
// Use this for initialization
void Start () {
enterArea = GetComponent<Collider2D>();
openAnimation = GetComponent<Animation>();
door1 = new Door(enterArea, openAnimation);
}
void OnTriggerEnter2D(Collider2D col)
{
Debug.Log("COL");
door1.open();
door1.isOpen = true;
}
void OnTriggerExit2D(Collider2D col)
{
Debug.Log("COLout");
door1.close();
door1.isOpen = false;
}
void Update () {
}
}
public class Panel : MonoBehaviour {
GameObject go;
Component comp;
Door thisDoor;
void Start () {
go = GameObject.Find("door");
Debug.Log(go.name);
DoorHandler door = go.GetComponent<DoorHandler>();
thisDoor = door.door1;
Debug.Log(thisDoor.isOpen);
}
void OnMouseDown()
{
if (thisDoor.isLocked)
{
thisDoor.unlockMe();
Debug.Log("unlock");
} else {
thisDoor.lockMe();
Debug.Log("lock");
}
}
void Update () {
}
}