0

I understand the following error message is caused when (in general) Unity is telling me that one of the things in line 54 of the myProg script doesn't exist:

NullReferenceException: Object reference not set to an instance of an object
myProg.OnGUI () (at Assets/Scripts/Editors/myProg.cs:54)

But as far as I can see and figure, there is nothing missing there in any way...

Here is the relevant part of the code snippet, and line 54 is the bit with .enabled (middle line):

if( GUILayout.Button("SART07Johnson") )
    {
        stage.setFatigueInductor( new Sart07JohnsonDefinition(GetComponent<ExperimentCreator>().getExperiment().getAvailableTableName("SART07Johnson")) );
        stage.setTypeOfFI( FatigueType.SART07Johnson );
        SART07JohnsonEditor editorSart07Johnson = GetComponent<SART07JohnsonEditor>();
        editorSart07Johnson.enabled = true;
        editorSart07Johnson.Init( (Sart07JohnsonDefinition)stage.getFatigueInductor(), this.stage.StageName,GetComponent<ExperimentCreator>().getExperiment().GetExperimentName() );
        GetComponent<NavigationTree>().CurrentEditor = editorSart07Johnson;
        this.enabled = false;
    }

So, I am wondering... what else could be the problem? Any specific other reason that I could look into, that might be causing this error message to be generated?

  • generally speaking the error messages do not 'lie' I suggest you post the actual code involved so that people can verify that it is not the code causing the problem. – MSB Nov 14 '14 at 15:22
  • You need to show us line 54 dude. –  Nov 14 '14 at 15:25
  • Thanks guys. I added that particular line, but the reason I hadn´t done so was because it would require me to post the other program that GetComponent is referencing... –  Nov 14 '14 at 15:37
  • 2
    The GameObject which this script is attached contains a "SART07JohnsonEditor" component? – Ricardo Reiter Nov 14 '14 at 15:47
  • Yes, The GetComponent refers to an existing C# script called SART07JohnsonEditor –  Nov 14 '14 at 15:52
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – CodeSmile Feb 16 '15 at 10:51

1 Answers1

0

You are right that it is telling you that an object doesn't exist. In this case, editorSart07Johnson is null and you are trying to get a property from it (.enabled). The line above calls GetComponent which according to the docs "Returns the component of Type type if the game object has one attached, null if it doesn't."

So the answer is that your game object does not have a component of type SART07JohnsonEditor attached.

Adam H
  • 1,523
  • 11
  • 21
  • But GetComponent calls SART07JohnsonEditor which means my game object now has SART07JohnsonEditor attached? Is that not right? –  Nov 14 '14 at 16:04
  • 1
    GetComponent won't attach components, it will only return a reference to components that are already attached. Use [AddComponent](http://docs.unity3d.com/ScriptReference/GameObject.AddComponent.html) to attach one in the first place. – Adam H Nov 14 '14 at 16:11