How do i use my setter?
This is a script that is attached to multiple "Chess Pieces" in my game and im calling the setter here.
public class GameManagerChess : MonoBehaviour {
public GameObject colliderObj=null;
public GameManage GameManager;
public int pieceIndex;
public int posX;
public int posY;
// Use this for initialization
void Start () {
this.transform.position = new Vector3(colliderObj.transform.position.x,colliderObj.transform.position.y,colliderObj.transform.position.z);
addToBoardPiecesTable ();
}
void addToBoardPiecesTable(){
GameManager = gameObject.GetComponent<GameManage>();
int pos = pieceIndex * int.Parse(this.tag);
GameManager.setBoardPieces (posX,posY,pos);
}
And this is the setter in the GameManage class
public void setBoardPieces(int posX,int posY,int pos){ _boardPieces[posX,posY]=pos; }
There is a "NullReferenceException: Object reference not set to an instance of an object" error in this line GameManager.setBoardPieces (posX,posY,pos);
Im trying to fill a table in the GameManage class by getting info from all the chess pieces. Im new to C# and unity3d.
Answer by pekalicious · Dec 09, 2015 at 03:08 PM
First, I recommend your variable names start with a lowercase so that there is a clear distinction between GameManager the class, and GameManager the variable.
Secondly, this
GameManager = gameObject.GetComponent<GameManage>();
assumes that the GameManager script is attached to the same game object the GameManagerChess is. Is this true?
If not, take a look at this short tutorial: GetComponent
Your answer
Follow this Question
Related Questions
A list of a struct doesn´t saw a word in the console. Object reference, but i dont have anyone 1 Answer
Object Reference Error 2 Answers
How to show an object based on timer variable in another script? 1 Answer
Why is object reference not set to an instance? 1 Answer
Random Chance NullReferenceException: Object reference not set to an instance of an object 2 Answers