unity3d - MissingReferenceException: Error after destroying a moving game object -


in game objects move every few seconds , others don't. have created method below handle movement of moving objects.

private ienumerator moveobject(gameobject movingobject, float posx, float duration) { if(movingobject != null) {     float elapsedtime = 0;     vector3 startpos = movingobject.transform.position;     vector3 endpos = new vector3(posx, objectpositiony, camera.nearclipplane);//hole.transform.position;      while(movingobject != null && elapsedtime < duration) {         movingobject.transform.position = vector3.lerp(startpos, endpos, elapsedtime / duration);         elapsedtime += time.deltatime;         yield return null;     } } } 

also destroy moving objects using code below:

for (int = 0; < holeobjects.count; i++) {     debug.log ("countingholeobjectindex"+ );      if (col.gameobject.equals (holeobjects [i].getholeobject ())) {        debug.log ("condition satisfied!" );         foreach( holeobjectsetup holeitem in holeobjects )        {           if (holeitem.getholeobjecttype () == holeobjectsetup.holeobjecttype.moving) {           int holeitemindex = holeobjects.indexof(holeitem);           holeitem.stopallcoroutines();           holeitem.destroyholeobject ();            //holeobjects.removeall(item=>item==null);           //holeobjects.removeat(holeitemindex);         }     } } 

any time destroy movingobject (mostly, when it's moving), error

missingreferenceexception: object of type 'gameobject' has been destroyed still trying access it. script should either check if null or should not destroy object. 

pointing line

movingobject.transform.position = vector3.lerp(startpos, endpos, elapsedtime / duration); 

in movingobject method (provided above). tried adding nullity check surround contents of while loop, made unity3d unresponsive, leaving me option of force quitting. how can resolve issue?

update movingobject called in coroutine below:

private ienumerator movementlogic(gameobject movingobject) {     // keep going until deactivated or disabled     float currentpos = movingobject.transform.position.x;     while(filledpositions.contains(currentpos))     {         float newpos = getnextpos(currentpos);                   if(freepositions.contains(newpos))             {                 freepositions.add (currentpos);                 filledpositions.remove (currentpos);                 freepositions.remove (newpos);                 filledpositions.add (newpos);                  yield return moveobject(movingobject, newpos, movementduration);                  //update positions                 yield return new waitforseconds(movementdelay);                 currentpos = newpos;              }             else             {                 // if valid hole wasn't found, try other direction next frame                 reversedirection();             }         }      yield return null;     //} } 

update 2

the code destroyholeobject() in holeobjectsetup is:

    public void destroyholeobject   ()  {     destroy (currentholeobject); } 

and

    public void createholeobjecttype    (gameobject holeobject, holeobjecttype holeobjecttype, vector3 holeobjectposition, bool iscollider2denabled)    {      currentholeobject = (gameobject)instantiate (holeobject, holeobjectposition, quaternion.identity);     setholeobjecttype (holeobjecttype);  } 

solve problem adding

debug.log 

statements everywhere. take few seconds find out exact point in cycle causing problems.

secondly,

sometimes cheap solution these problems is, use

destroyimmediate 

rather than

destroy 

this might give quick fix.

thirdly. more sophisticated coroutines:

something ..

void onenable()  {  debug.log("enable ..");   }  void ondisable()  {  stopcoroutine("whatever");  .. or perhaps ..  stopallcoroutines();  } 

moreover, ideally should "handle manually".

consider object getting rid of. should have function

void beingdestroyed()  {  .. write own code here shut down object ..  } 

... , call explicitly when destroy it.

(indeed, may prefer let object destroy itself: perform needed shutdown code, wait frame or 2 if relevant you, remove lists, , destroy self.)

you can't "leave coroutines alone", have manage them hand.


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -