1

I keep getting this error when I click the Card so I can move it.

NullReferenceException: Object reference not set to an instance of an object CardProspector.OnMouseUpAsButton () (at Assets/__Scripts/CardProspector.cs:17) UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32, Int32)

The exception indicates the following method in my CardProspector class:

override public void OnMouseUpAsButton() {
    Prospector.S.CardClicked(this);  // line 17
    base.OnMouseUpAsButton();
}

But that variable should have been instantiated by my Prospector class, the relevant portion of which is shown below:

public class Prospector : MonoBehaviour {
    static public Prospector S;
    ...
    ...

    // Use this for initialization
    void awake() {
        S = this;
    }

    ...
    ...
}
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
  • 1
    i guess that awake has not been called so S is null – pm100 Apr 06 '15 at 20:42
  • Side note: Providing small piece of code that demonstrates the problem makes for better SO questions. As with all debugging questions [rubber duck debugging](http://en.wikipedia.org/wiki/Rubber_duck_debugging) is useful technique... – Alexei Levenkov Apr 06 '15 at 21:03
  • 1
    @GrantWinney looks good to me - reopened. Comment will self-destruct in 10.. 9... – Alexei Levenkov Apr 06 '15 at 21:51

2 Answers2

3

Awake() is case sensitive and will never be called in your code.

jan
  • 865
  • 7
  • 19
0

This part of the message gives away where the problem is:

(at Assets/__Scripts/CardProspector.cs:17)

That ":17" means line 17 of your CardProspector.cs file, which translates to the following line:

Prospector.S.CardClicked(this);

That would suggest that static property "S" is null--it hasn't been defined.

Since "S" is defined in method Awake(), you need to call Awake() before you reference property "S" in any way.

Russ
  • 4,091
  • 21
  • 32