Hi, so I don’t understand where the error is. This is turning me crazy. Can anyone help?
If you read the line that the error refers, there is nothing wrong.
And obviously, I have a Scrpit attached to the same Game Object called “PickableObject” with a bool called “isPickable”.
The error: NullReferenceException: Object reference not set to an instance of an object
GunWaiting.Update()(at Assets/Scripts/GunWaiting.cs 21).
The code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GunWaiting : MonoBehaviour
{
float speed = 4.0f;
void Update()
{
if (GetComponent<PickableObject>().isPickable == true) { transform.Rotate(0f,0f,-speed); }
}
}
Try creating a public instance variable for PickableObject and see if it’s possible to assign it through the inspector.
One reason this might be happening is if the filename of PickableObject does not match its class name. Then you might see the component on the GameObject in the inspector (if you added it before the class name or filename was changed), but it is still not a valid component that can be referenced in code.
Debug.Log is your best friend. Quadruple check if the component is on the Game Object. Only thing on that line that would be null is the GetComponent call. Had this issue the other day and checked, low and behold I forgot to add the component.
Could you share what it was that solved your problem? It might help someone else who runs into the same issue, if they can find the solution from this thread.