- Home /
I am having problems with a door collider and a trigger.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class DoorOpen : MonoBehaviour {
private Animator animator;
private bool doorOpen;
void start()
{
doorOpen = false;
animator = GetComponent<Animator>();
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Key"))
{
doorOpen = true;
Doors ("Open");
}
}
private void OnTriggerExit(Collider other)
{
if (doorOpen)
{
doorOpen = false;
Doors ("Close");
}
}
void Doors (string direction)
{
animator.SetTrigger(direction);
}
} this is my code and it is saying
NullReferenceException: Object reference not set to an instance of an object DoorOpen.Doors (System.String direction) (at Assets/Animations/DoorOpen.cs:40) DoorOpen.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Animations/DoorOpen.cs:23)
The key has a tag called "Key" and the door has a collider that you need to bring the key to to open the door. Any ideas?
Answer by Sabre-Runner · Nov 04, 2018 at 03:40 AM
It is not clear from your question to which line of code this actually refers too. But if I grok this correctly, it looks like your Animator assignment isn't taking. Either make sure it's grabbing correctly, check it before you use it, or forgo the GetComponent and assign it in the inspector.
How would I execute that by checking if it is grabbing it correctly? Or how would I assign the item in the inspector? Do I use a public variable?
The preferred method is [SerializeField] private This will show them in the inspector but won't make it otherwise accessible.
Your answer
Follow this Question
Related Questions
Open door with key 2 Answers
Trying to open a door so far then stop 1 Answer
Open & Close a simple door animation with sound and door states 1 Answer
Puzzle open door ? 1 Answer
open and close door 0 Answers