c# - OnMouseDown event, NullReferenceException: Object reference not set to an instance of an object -
im new unity3d c#. im trying make simple puzzle15 game.
when click on each tile trying current tile coordinates , empty tile coordinates in order move current tile. tile objects instantiates in gameboard.cs class has move() method , trying run method onmousedown() method class tileparams.cs created store coordinates each tile. tileparams.cs attached tileprefab instantiate tiles from.
i`ll not give of code, part getting error from.
public class gameboard : monobehaviour { ... ... ... void instantiatetile(int tilenum, int x_coord, int y_coord) { settileposition (x_coord, y_coord); tileobject [tilenum] = instantiate (tileprefab, new vector3 (tile_x, tile_y, 0), tileprefab.transform.rotation) gameobject; tileobject [tilenum].transform.findchild ("tilenumber").gameobject.getcomponent<textmesh> ().text = tilenum.tostring(); tileobject [tilenum].getcomponent<tileparams> ().setx_coord (x_coord); tileobject [tilenum].getcomponent<tileparams> ().sety_coord (y_coord); } void puttilestogameboard() { ... ... ... instantiatetile (tilesquantity, x_coords[tilesquantity], y_coords[row]); // instantiating empty tile tileobject[tilesquantity].transform.findchild("tilenumber").gameobject.getcomponent<textmesh>().text = ""; tileobject [tilesquantity].getcomponent<meshrenderer> ().enabled = false; debug.log (tileobject [tilesquantity].getcomponent<tileparams> ().getx_coord ()); // here works (int tilenum = 1; tilenum < tilesquantity * tilesquantity; tilenum++) { if (tilenum % tilesquantity == 0) { row++; col = 1; shufflearray(x_coords); } instantiatetile(tilenums[tilenum], x_coords[col], y_coords[row]); //debug.log(tilenum + "\t" + x_coords[col] + "\t" + y_coords[row]); col++; } } public void move(tileparams currenttile){ debug.log (tileobject [tilesquantity].getcomponent<tileparams> ().getx_coord ()); //here getting error }
and tileparams.cs class:
public class tileparams : gameboard { private int x_coord; private int y_coord; ... ... ... public void setx_coord(int x_coord){ this.x_coord = x_coord; } public void sety_coord(int y_coord){ this.y_coord = y_coord; } public int getx_coord(){ return x_coord; } public int gety_coord(){ return y_coord; } void onmousedown (){ move (this); }
so,when click on each tile throws "nullreferenceexception: object reference not set instance of object ". appreciated. thank you.
Comments
Post a Comment