c# - GameObject array returns null when called from another class -
this question has answer here:
- what nullreferenceexception, , how fix it? 29 answers
i in lost, gameobject returns null.
about issue, i'm unsure if it's because missed small thing, or if i'm straight programming use of multiple classes wrong.
my code generate gameobject array:
public class gridinit : monobehaviour { const int mapwidth = 33; const int mapheight = -27; const float tilesizeunits = 1.0f / 3; public gameobject tile; // use initialization void start () { generatetilemap(); screen.fullscreen = true; } void awake() { } // update called once per frame void update () { } bool walkable = false; [hideininspector] public gameobject[] gotile = new gameobject[892]; void generatetilemap() { int tilenumber = 0; (float y = 0; y > mapheight; y--) { (float x = 0; x < mapwidth; x++) { float tileposx = x/3; float tileposy = y/3; int layerpos = -2; gotile[tilenumber] = instantiate(tile, new vector3(tileposx, tileposy, layerpos), quaternion.identity) gameobject; gotile[tilenumber].name = "tile" + tilenumber; gotile[tilenumber].tag = "grid"; raycasthit2d hit; vector3 fwd = gotile[tilenumber].transform.transformdirection(vector3.forward); debug.drawray(gotile[tilenumber].transform.position, fwd * 50, color.green); hit = physics2d.raycast(gotile[tilenumber].transform.position, fwd, 200); if (hit) { gotile[tilenumber].tag = "walkablegrid"; } tilenumber++; } } }
and class use set gameobjects:
public class gamecalibration : monobehaviour { bool calibrationcomplete = false; public gridinit ginit; public cam cam; // use initialization void start () { } // update called once per frame void update () { { (int = 0; < ginit.gotile.length; i++) { if (calibrationcomplete == false) { ginit.gotile[i].getcomponent<renderer>().enabled = true; } else { ginit.gotile[i].getcomponent<renderer>().enabled = false; } } if (calibrationcomplete == false) { calibrate(); } else { ginit.generateobjects(); calibrationcomplete = true; } } } vector2[] camtilecenter; vector2 tempcenter; void calibrate() { (int = 0; < ginit.gotile.length; i++) { ginit.gotile[i].getcomponent<spriterenderer>().color = color.red; if (cam.redblobs[i] != null && camtilecenter[i] != tempcenter) { camtilecenter[i] = new vector2(cam.redblobs[i].width / 2, cam.redblobs[i].height / 2); tempcenter = camtilecenter[i]; } } } }
i've tried messing around values , how class initialized, no prevail.
my complete error:
nullreferenceexception: object reference not set instance of object gamecalibration.update () (at assets/scripts/gamecalibration.cs:21)
error line in gamecalibration: ginit.gotile[i].getcomponent<renderer>().enabled = true;
public gridinit ginit = new gridinit();
instead of
public gridinit ginit;
i dont see creating instance of object anywhere
Comments
Post a Comment