There are three scripts(class) “Boardmang”, “ChessManAbstract”, “pawn”.
“pawn” is inheriting the “ChessManAbstract” script which consists the information of each object of a chessboard that which piece is where and what is its right move for that piece.
And the pawn script is overriding one method “possibleMoves”.
Now there is a “public static Instance Boardmang{set; get;}”(instance) being created in the “Boardmang” script.
And the public static instance of “Boardmang” is being used in the method which is overriding the “possibleMoves” and I have also initialized the Instance properly (using “Instance = this;”);
Problem: Now the main problem is that even after initializing the Instance properly when used in another script
that static Instance is showing “NullReferenceException: Object reference not set to an instance of an object”.
You first need to use either the debugger or Debug.Log to actually figure out which value is null. Is it Boardmang.Instance, or is it the Chessmans array?
If it is Boardmang.instance, do you actually have a Boardmang object in the scene anywhere?
Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.
Unity can serialize neither multi-dimensional arrays nor properties. Chessmans is both of them. What this means is that you as the developer are responsible for making sure that there is an object and not null.
I can see that in SpawnAllChessmans(), you are creating the array, but are you also assigning objects to them? Assuming that you only spawn chess objects where they are appropriately used (so not on every field and thus element in the array), checking conditions like this won’t work:
if(!c.isWhite && c != null)
You have to check for null first, then check the properties of the object. Right now, this IF-condition translates to "Check if the value of the property isWhite of object c is true and if c exists.
You check the property of an object that might not exist, because the order matters when a boolean condition is evaluated. You have to check first if the object exists, then check the values of its properties: