Object reference not set to an instance of an object unityengine debug logerror object

Topic: NullReferenceException: Object reference not set to an instance [SOLVED]  (Read 4355 times)

i have been working on Tutorial 12 - Connecting Scripts to Playmaker

The Cubes work great...

however the Random button always sets off this error message in Unity at line 11 & 18

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

i am quite confused about this error and hope you could offer some insight

i have not changed your code and downloaded it from your website

i am using Unity 2019.3.0f6

your c# code i used is as follows:

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

public class RandNumButton : MonoBehaviour
{
    private PlayMakerFSM RandNumFSM;

    void Start()
    {
        RandNumFSM = GameObject.Find("GUITEXT_RandNum").GetComponent<PlayMakerFSM>();
    }

    public void OnGUI()
    {
        if (GUI.Button(new Rect(295, 145, 75, 40), "Rand"))
        {
            RandNumFSM.Fsm.Event("set_number");
        }
    }
}

Line 11 & 18 are highlighted

« Last Edit: February 14, 2020, 05:58:51 AM by jeanfabre »

Object reference not set to an instance of an object unityengine debug logerror object
Logged


Hi,

It's because you haven't got an object in your scene exactly named "GUITEXT_RandNum", double check that please.

 also, make a test case to check if RandNumFSM is null or not and log your own error, then you will be sure about what's happening.

Bye,

 Jean

Object reference not set to an instance of an object unityengine debug logerror object
Logged


i am new to this however i am confused because i do believe i have a game object called GUIText_RandNum

i am using your scene download

Object reference not set to an instance of an object unityengine debug logerror object
Logged


it is what the FSM is attached to

« Last Edit: February 11, 2020, 01:36:21 AM by Rene »

Object reference not set to an instance of an object unityengine debug logerror object
Logged


i got it to work however I am not sure i understand why it works...

i altered the c# script "RandNumButton" as follows...

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

public class RandNumButton : MonoBehaviour
{
    public PlayMakerFSM RandNumFSM;

    //void Start()
    //{
    //    RandNumFSM = GameObject.Find("GUITEXT_RandNum").GetComponent<PlayMakerFSM>();

    //    if (RandNumFSM == null)
    //    {
    //        Debug.Log("RandNumFSM is null!");
    //        return;
    //    }
    //}


    public void OnGUI()
    {
        if (GUI.Button(new Rect(295, 145, 275, 40), "Random Number Generator"))
        {
            RandNumFSM.Fsm.Event("set_number");
        }
    }
}

i also had to make RandNumFSM public and then attach the GUIText_RandNum GameObject to the RandNumFSM variable as shown in the attachment image of the GUIText_RandNum inspector...

i am not sure how i did this (complete fluke) and am not even sure if this is the proper way to do this...

please if you have any insight i would be grateful

« Last Edit: February 11, 2020, 02:20:55 AM by Rene »

Object reference not set to an instance of an object unityengine debug logerror object
Logged


i realize that using IMGUI is not really the best way to use buttons however to make this work i am not sure why i don't need to use the GetComponent

i have been altering my script however the following script does not work...

i just wonder if you have any thoughts about why this breaks the button?

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

public class RandNumButton : MonoBehaviour
{
    public PlayMakerFSM RandNumFSM;

        void Start()
        {
            if(RandNumFSM = null)
            {
                Debug.Log("RandNumFSM was null!");
                RandNumFSM = GameObject.Find("GUITEXT_RandNum").GetComponent<PlayMakerFSM>();
            }
    }

    public void OnGUI()
    {
        // Make a background box
        GUI.Box(new Rect(10, 10, 400, 90), "Tutorial 12 - Connecting Scripts to Playmaker Menu");

        // Make the first button. If it is pressed, Application.Loadlevel (1) will be executed
        if (GUI.Button(new Rect(20, 40, 80, 20), "Click Me"))
        {
            Debug.Log("Hello Rene!");
        }

        // Make the second button.
        if (GUI.Button(new Rect(20, 70, 200, 20), "Random Number Generator"))
        {
            NewMethod();
        }

        //   if (GUILayout.Button("Press Me"))
        //       Debug.Log("Hello!");

        //   if (GUILayout.Button("Random Number Generator"))
        //{
        // RandNumFSM.Fsm.Event("set_number");
        //}
    }

    private void NewMethod()
    {
        if (RandNumFSM = null)
        {
            Debug.Log("RandNumFSM was null!");
            RandNumFSM.Fsm.Event("set_number");
        }
    }
}

Object reference not set to an instance of an object unityengine debug logerror object
Logged


I just tried making the playmakerFSM variable called RandNumFSM PRIVATE and and ran this code...and it still is broken...Making the variable private prevents me from manually hooking up the gameobject to the c# script from the inspector...it seems the only way to make this work is to manually hook up the gameobject (GUIText_RandNum) and to create the PlaymakerFSM variable as public...

I tried putting the event call in a separate Method...this confuses me and I wish to know if you could test my code and provide insight please

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

public class RandNumButton : MonoBehaviour
{
    private PlayMakerFSM RandNumFSM;

        void Start()
        {
            if(RandNumFSM == null)
            {
                Debug.Log("RandNumFSM was null!");
                RandNumFSM = GameObject.Find("GUITEXT_RandNum").GetComponent<PlayMakerFSM>();
            }
    }

    public void OnGUI()
    {
        // Make a background box
        GUI.Box(new Rect(10, 10, 400, 90), "Tutorial 12 - Connecting Scripts to Playmaker Menu");

        // Make the first button. If it is pressed, Application.Loadlevel (1) will be executed
        if (GUI.Button(new Rect(20, 40, 80, 20), "Click Me"))
        {
            Debug.Log("Hello Rene!");
        }

        // Make the second button.
        if (GUI.Button(new Rect(20, 70, 200, 20), "Random Number Generator"))
        {
            NewMethod();
        }

        //   if (GUILayout.Button("Press Me"))
        //       Debug.Log("Hello!");

        //   if (GUILayout.Button("Random Number Generator"))
        //{
        // RandNumFSM.Fsm.Event("set_number");
        //}
    }

    private void NewMethod()
    {
        if (RandNumFSM == null)
        {
            Debug.Log("RandNumFSM was null!");
            RandNumFSM.Fsm.Event("set_number");
        }
    }
}



Object reference not set to an instance of an object unityengine debug logerror object
Logged


Hi.

 this is what you need to do:


var _go = GameObject.Find("GUITEXT_RandNum")
           
if(_go == null)
{
  Debug.LogError("not object <GUITEXT_RandNum> found in the scene!");
}else{

 RandNumFSM = RandNumFSM.GetComponent<PlayMakerFSM>();
if(RandNumFSM == null)
{
Debug.LogError("not playmakerFSM component found in the the object!",_go);
}
}

when you use GameObject.Find() api, you must always check that the result is not null, cause in your case, it's likely that there are no objects named "GUITEXT_RandNum" in your scene.

Bye,

 Jean

Object reference not set to an instance of an object unityengine debug logerror object
Logged


Hello Jean,

Thank you for your patience with me.

I apologize for my lack of understanding but I will keep trying to learn.

I hope I did not ask too much.

I will test this code.

Rene

Object reference not set to an instance of an object unityengine debug logerror object
Logged


Am I missing something?

I sent you a picture of my game object earlier in the message thread and it exists in the scene and the hierarchy

Why can’t I understand this issue?

So when I test your code it goes through to the  debug log and the code does not work

Could I please send you my project for you to run?

I will make two scenes one with the code you showed me and another with what I did to make it work

Object reference not set to an instance of an object unityengine debug logerror object
Logged


Finally Jean... Please forgive me but I finally discovered the mistake that I have been making and I do not have to send you my project!

Your code is working!

Thank you for helping me and this now makes sence!

Object reference not set to an instance of an object unityengine debug logerror object
Logged


Object reference not set to an instance of an object unityengine debug logerror object
Logged


How do I fix this error object reference not set to an instance of an object in unity?

To fix this example we can acquire a reference to an instance of the script using GameObject. Find to find the object it is attached to. We then use GetComponent to find the script component we want a reference to. You can also double click the error to take you to the line of script where the error is occurring.

How do you fix object reference not set to an instance of an object?

How to Avoid Object Reference Not Set to an Instance of an Object?.
Explicitly check for null and ignore null values. ... .
Explicitly check for null and provide a default value. ... .
Explicitly check for null from method calls and throw a custom exception. ... .
Use Debug..

How do I fix null reference exception in C#?

You can eliminate the exception by declaring the number of elements in the array before initializing it, as the following example does. For more information on declaring and initializing arrays, see Arrays and Arrays. You get a null return value from a method, and then call a method on the returned type.