0

This is the full error (in this new scenario, it is a NullReferenceException):

NullReferenceException: Object reference not set to an instance of an object
FightScript.startFight_Click () (at Assets/Scripts/FightScript.cs:105)
UnityEngine.Events.InvokableCall.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:166)
UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:58)
UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:36)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:45)
UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50)
UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
UnityEngine.EventSystems.EventSystem:Update()

Here is the code:

public void startFight_Click()                // line 100
{   
    ...
    Debug.Log(strengthHeroValue.ToString());  // line 105 here
    ...
}

strengthHeroValue is declared at the class level as public like this:

public Text strengthHeroValue;

In the UI, strengthHeroValue is assigned to a ValueText in the FightScript (Script) section.

I have a button called startButton and in the On Click() section, I chose FightController and then FightScript.startFight_Click() event.

When I double click on the error, it brings me to the startFight_Click() method which is line 100.

Thanks for your help.

memowe
  • 2,617
  • 13
  • 25
  • 1
    Which precise line is throwing the `NullReferenceException`? I'd be very, very surprised if it were actually the line `name = "George";`. Perhaps the line numbers are being messed up somehow? – Jon Skeet Nov 13 '18 at 18:57
  • 2
    That error means that you're trying to use the object before it has a value. I have a feeling we're not seeing an accurate representation of the code here. – MikeH Nov 13 '18 at 18:57
  • 3
    As a side node: The `MonoBehaviour` class already has a `public string name;` member, so yours is hiding the one from your base class. For a start, try changing you `name` fields name and see if things get clearer. – Thomas Hilbert Nov 13 '18 at 18:57
  • You say you can't access the member. What error do you get when you try? – Thomas Hilbert Nov 13 '18 at 19:07
  • 2
    Is it possible that Unity is creating an unbound delegate? `startFight_Click` looks like a `Click` event handler on a `startFight` field. If Unity is doing the event binding through reflection but `startFight` hasn't been initialized, it could *in theory* create an unbound delegate. The call pushes null onto the stack just fine, but within the method, the ldarg.0 instruction pushes the null, and then the stfld instruction throws the exception. Try putting a breakpoint on the `{` and taking a look at `this` and `startFight` in the watch window. – madreflection Nov 13 '18 at 19:07
  • 3
    Please explain your issue. If there is an error double-click on the error then post the line of code that's causing it. It makes no sense to say that `name = "George";` is causing an error. Double-click on the error then post the correct code on the line that's causing that error – Programmer Nov 13 '18 at 19:08
  • Sorry, *open* delegate. And now that I think of it, if `startFight` is initialized later, it won't be null in the watch window when you hit the breakpoint. – madreflection Nov 13 '18 at 19:17
  • Are you assigning a prefab's method to the button somehow? – Ruzihm Nov 13 '18 at 22:13
  • How exactly is `startFight_Click` called? – derHugo Nov 14 '18 at 06:29
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Eliasar Nov 14 '18 at 16:48

1 Answers1

1

I can't proof it, but this is the only reasonable explanation I can think of considering the hints in your comments. So here goes:

The NullReferenceException is not thrown because you are trying to access name inside starFight_Click. It is thrown while your onClick event tries to call starFight_Click. The onClick event does not have a valid reference to your script instance but tries to call the startFight_Click on null what causes the NullReferenceException. Check your event handler.

Thomas Hilbert
  • 3,438
  • 2
  • 11
  • 32
  • 5
    Not sure why everyone is talking about "NullReferenceException" in the answer and comment section. OP never said "NullReferenceException" is the problem.... – Programmer Nov 13 '18 at 19:04
  • Fair point..! I read it in the comments and didn't even notice it wasn't in the question in the first place. – Thomas Hilbert Nov 13 '18 at 19:06
  • 2
    @Programmer "Object reference not set to an instance of an object" was typed in the code comments. Maybe that's what they're meaning. – Eliasar Nov 13 '18 at 19:06
  • 1
    aah, there it is. Thx Eliasar xD – Thomas Hilbert Nov 13 '18 at 19:07
  • The variable name was only used as an example to get the error. You were right, name is a variable used by MonoBehaviour. By changing the variable name, i didn't get the error. – Michel Tremblay Nov 14 '18 at 13:02