Opening a door outwards from the player whenever a player is colliding it.c#
I tried looking around and cant seem to find any door related to be similar to what I am looking for. Would like to ask for assistance on this.
using UnityEngine;
using System.Collections;
public class newdooropen : MonoBehaviour
{
Animator anim;
bool doorOpen = false;
bool playerstillhere = false;
void Start() {
anim= GetComponent<Animator>();
}
void Update()
{
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player") {
if (playerstillhere==false)
{
if (doorOpen==false)
{
doorOpen = true;
}
else
{
doorOpen = false;
}
playerstillhere = true;
}
}
anim.SetBool("PlayerIsThere", playerstillhere);
anim.SetBool("DoorStatus", doorOpen);
}
void OnTriggerExit(Collider another)
{
if (another.tag == "Player")
{
playerstillhere = false;
anim.SetBool("PlayerIsThere", playerstillhere);
}
}
}
This keeps giving me an error. NullReferenceException: Object reference not set to an instance of an object newdooropen.OnTriggerEnter (UnityEngine.Collider other) (at Assets/newdooropen.cs:43)
Comment
anim = GetComponent<Animator>();
$$anonymous$$ost likely this returns null. It's always good to check for null and write a message to a console if something went wrong.
Does the gameobject this script is attached to have an Animator component?