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.

10
  • 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? Commented Nov 13, 2018 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. Commented Nov 13, 2018 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. Commented Nov 13, 2018 at 18:57
  • 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. Commented Nov 13, 2018 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 Commented Nov 13, 2018 at 19:08

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Not sure why everyone is talking about "NullReferenceException" in the answer and comment section. OP never said "NullReferenceException" is the problem....
Fair point..! I read it in the comments and didn't even notice it wasn't in the question in the first place.
@Programmer "Object reference not set to an instance of an object" was typed in the code comments. Maybe that's what they're meaning.
aah, there it is. Thx Eliasar xD
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.