- Home /
The question is answered, right answer was accepted
Object reference not set to an instance of an object
Hi, so I made it where when my player collides with an object with the tag collider it will disable the object to make it look like they picked it up, but it keeps giving me this error, NullReferenceException: Object reference not set to an instance of an object DuckController.OnTriggerEnter (UnityEngine.Collider other) (at Assets/world/DuckController.cs:47) DuckController.FixedUpdate () (at Assets/world/DuckController.cs:22), I have no idea why. Thanks for the help!
Code:
[SerializeField] float Speed = 3.5f;
Rigidbody rigidbody;
Collision collision;
Collider other;
// Use this for initialization
void Start () {
rigidbody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate() {
Invoke("Rotate", 2f);
OnTriggerEnter(other);
}
void Rotate()
{
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
transform.eulerAngles = new Vector3(0, 180, 0);
}
else if (Input.GetKeyDown(KeyCode.RightArrow))
{
transform.eulerAngles = new Vector3(0, 0, 0);
}
else if (Input.GetKeyDown(KeyCode.UpArrow))
{
transform.eulerAngles = new Vector3(0, -90, 0);
}
else if (Input.GetKeyDown(KeyCode.DownArrow))
{
transform.eulerAngles = new Vector3(0, 90, 0);
}
rigidbody.AddForce(transform.forward * Speed);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == ("collect"))
{
other.gameObject.SetActive(false);
}
}
}
Answer by pako · Feb 23, 2018 at 06:39 PM
OnTriggerEnter(Collider)
is called automatically by Unity, with the correct Collider as parameter of the method.
In your case, you declare a private class variable Collider other
, which you never set, so it's null, and you pass it as the parameter to the OnTriggerEnter(Collider)
method, but since you never set the value of other
, you just pass null
.
This is wrong usage. Let Unity do it's job.
Remove the Collider other
variable declaration, and don't call OnTriggerEnter(Collider)
from anywhere in your code. Unity will call it, and you just need to declare the method (as you have already done).
Answer by Deathdefy · Feb 23, 2018 at 05:45 PM
OnTriggerEnter executes when a collider hits another collider that is marked trigger. You dont need to call it from FixedUpdate as well.
Use this.
void FixedUpdate() {
Invoke("Rotate", 2f);
}
https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html
Thanks for the reply! What do you mean? I know that is what OnTriggerEnter does I want it to do this. I removed fixed update and put this under rotate and it still did not work.
In the inspector on your Collider component, there is a property called IsTrigger. That must be checked for OnTriggerEnter to execute. If you dont have a collider that ISNT marked IsTrigger and one that IS marked IsTrigger, this method will never execute.
So, whatever object has this script on it needs to also have a Collider attached that has the property IsTrigger checked to True.
I already had it as IsTrigger, thanks though it is fixed now.