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

Hello, I have a small problem and I will try simpilfy this as much as I can.

I have hierarchy like this: Imgur: The magic of the Internet (sorry for link, I don’t know how to upload image correctly here), I attached two scripts (script1 and script2) to Mechanics.


The script1 have Function1 which is attached to ‘on click’ of button “Key” (technically this is prefab and I’m using more buttons, each of them using the same function).
The Function1 (from script1) starts the Function2 (within the script2) by GetComponent<script2>().Function2();

and this is where the problem is, I’m getting error “NullReferenceException: Object reference not set to an instance of an object”. Not sure how to deal with it

No images. Please upload the images again to your question.

Is your script2 placed on the same gameobject as script1? Is it active and enabled?

Is this the actual line that has the error? If so, that would mean that GetComponent couldn’t find a component called script2 on your game object.

If GetComponent doesn’t find the specified component, it will return “null” which is nothing. Since “null” does not have a method called Function2 (or any method at all), you get an error message.

@palex-nx Yes, this is Inspector of game object “Mechanics” Imgur: The magic of the Internet (they are script1 and script2)
@kdgalla if I try to use function that doesn’t exist Im getting error "function2 does not contain a definition for ‘scriptthatdoesnnotexist’ and no accessible extension method ‘scriptthatdoesnnotexist’ accepting a first argument of type ‘function2’ could not be found " so I can’t even start the program.

In my project I can start the “game” (compilator see no errors) and the error appear after pressing the button (the button runs the Function1 and the Function1 trying to run the Function2 but it can’t and giving the error)

1 Like

Show us the complete scripts 1 and 2. But more important show the Inspector of the on click button.
What did you put in the on click button event ?

You right if the function exist then the script2 is exist and you typed it right :

GetComponent<script2>().Function2();

Maybe something is missing or wrong with the on click event in the button ?

The difference is that “null” is not an object. The error message you just posted is the message you get when you have an object but it doesn’t have a method that you’re trying to call.

If you try to call a method and the object doesn’t exist (i.e. it’s null) then you get a “null reference exception”

Use this site to upload images I have the same problem like you with uploading images.
The only site that work for me is : http://tinypic.com/

Just click the Choose File and then UPLOAD NOW! and then in the end copy the image link the most bottom link to here.

Click here on the image and add the link.

I think I found the problem.
I forgot that script1 is attached to button too (and there is no script2) and it doesn’t see script2 by GetComponent then. Anyway, in this situation how can I run the function2 (script2 is out of the object where script1 is)?

There are at least two ways I can think of. The easiest way is to add a public member in script1:

public script2 myscript2instance;

This will create a blank in the inspector for script1 that you can just drag your scene object into and it will link the object’s script2 to your member variable.

Also this tutorial video may be useful for you. It demonstrates different ways that different scripts can communicate:

I see, this works but I’m creating buttons from prefabs ‘in real time’ and I’m not able to drag this object manually. Can I do this using script?

1 Like

Add the script2 to the same GameObject where script1 is attached to.
Now when you click the button you don’t get the null exception and you don’t need a reference from script1 to script2.
And no need to drag it in real time.

If I attach the script2 to button I must drag something else anyway so the problem is the same, its just…delayed.

I must place some Text as a “Word”. This is prefix and my ‘drag and drop’ is not applicated to buttons created from this prefix (when I attach some Text object to this script obviously) :confused:

Dont drag any of the scripts to the button.

Look at my screenshot again.
I added to the Hierarchy a new empty GameObject (You can rename it) and tho this empty gameobject I attached the two scripts Script1 and Script2. Not to the button.

To the button it self to the event I dragged the empty GameObject with the two scripts :

You can see that I dragged the GameObject to the event of the button and then on the right when I click I see Script1 and Scrip2 and in Script1 you have the Function1

Again the two scripts attached to the empty GameObject see my first screenshot. Then you drag the GameObject to the button event.

For the Text problem :

And this is Script2 for example :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Script2 : MonoBehaviour
{
    public Text Word;

    public void Function2()
    {
        Debug.Log("Function2");
    }
}

You then drag the GameObject to the Text place. Or you can click in the editor menu

GameObject > UI > Text

And then it will add a Text under the button and then you can drag this Text to the script in the GameObject.

I missed what do you want to do with the Text (Word) ?

@dubiduboni_unity Okay, I see your point. Unfortuneally I cannot override those changes. After override - the changes are not applayed to prefix and any button created from this prefix.

Download this package and see if it’s working like you want.

4474435–411268–ButtonTest.unitypackage (4.72 KB)

It works but just for one button. I mean this is solving my problem but creating another: I’m trying override those changes to all buttons but I can’t

here, I attached the Mechanics (GameObject from your example) to button, set the right function in On Click field and when I’m pressing Overrides>Apply All, changes are not applied to all the buttons

What is this Override ? Do you have more then one button ? I tried now with two buttons added a new button and also used the event with Function1 in the second new button and it didn’t ask me to Override.