c# - Deleting the last Instantiated GameObject from a List and Scene -
so i'm missing that's pretty straightforward some, i'm having trouble it. i've created list (msgsymbols) gets populated new gameobject every time button gets pressed (it's custom keyboard) gets instantiated grid layout.
but how delete last gameobject on list , update grid layout reflect change? (like hitting backspace button on keyboard)? delete key code have throwing error: argumentoutofrangeexception: argument out of range. parameter name: index
how resolve this?
public string prefabpath; list<gameobject> msgsymbols = new list<gameobject>(); vector3 symbolpos = new vector3(0, 0, 0); gameobject currentchar; gameobject msgpanel; vector3 symbolscale = new vector3(1.0f, 1.0f, 1.0f); gameobject[] charkeys; gameobject deletekey; private int index = 0; void start() { msgpanel = gameobject.findgameobjectwithtag("messagepanel"); charkeys = gameobject.findgameobjectswithtag("symbolkey"); deletekey = gameobject.findgameobjectwithtag("deletekey"); } #region ipointerclickhandler implementation public void onpointerclick (pointereventdata eventdata) { if (transform.comparetag("symbolkey")) { // load gameobject msgsymbols list // , store last character added in variable (lastchar) msgsymbols.add((gameobject)resources.load(prefabpath)); currentchar = msgsymbols.last<gameobject>(); // instantiate last character (lastchar) added msgsymbols list gameobject symbol = instantiate(currentchar, symbolpos, quaternion.identity) gameobject; // define transforms symbol symbol.transform.setparent(msgpanel.transform); symbol.transform.localscale = symbolscale; } if (transform.comparetag("deletekey")) { debug.log (currentchar); gameobject todestroy = msgsymbols[msgsymbols.count - 1]; msgsymbols.removeat(msgsymbols.count - 1); destroy(todestroy); debug.log(msgsymbols.count); } } #endregion
it seems problem have gameobject
isn't going away when want rid of it. don't know how code aren't showing here works, seems possible removing object list, doesn't have effect on scene because gameobject
still exists part of scene. removing list won't have effect on default. try calling destroy
on object after removing list.
debug.log (lastchar); gameobject todestroy = msgsymbols[msgsymbols.count - 1]; msgsymbols.removeat(msgsymbols.count - 1); destroy(todestroy); debug.log(msgsymbols.count);
Comments
Post a Comment