NullReferenceException: Object reference not set to an instance of an object

You should use code tags in the future when posting code to make it easier to read.

It looks like GameObject.Find(“denovo”) does not find a GameObject with such a name in the scene and returns null. Then when you try to call GetComponent() on something that does not exist, you get a NullReferenceException.

Finding GameObjects using GameObject.Find is considered bad practice, and it very prone to errors (as you are just finding out). It is a better idea to add the [SerializeField] attribute to the denovo field and then assign the reference using the inspector if it is possible in your case.

If this is not possible, you can create a unique component for your “denovo” button, let’s say DenovoButton, and then you can use FindObjectOfType() to find the reference.

1 Like