- Home /
Cannot Destroy() object, created via GameObject.CreatePrimitive (others destroyed as should)
Hi all. I'm completely new to Unity, installed it yesterday. I tried to create a cube via GameObject.CreatePrimitive(PrimitiveType.Cube); and then destroy it. And it don't disappears. When I doing the same code with object, placed on scene in editor or with object, gained from Instantiate, it destroys them as should.
Sample code to reproduce bug.
var tmp : GameObject;
function Update ()
if(Input.GetKey(KeyCode.Z))
{
tmp = GameObject.CreatePrimitive(PrimitiveType.Cube);
};
if(Input.GetKey(KeyCode.X))
{
UnityEngine.GameObject.Destroy(tmp.gameObject);
};
if(Input.GetKey(KeyCode.C))
{
UnityEngine.GameObject.Destroy(gameObject);
};
}
if I add it to object on scene, C deletes the object, but X do not delete the cube created by Z.
Hope for your help!
Answer by Eric5h5 · Apr 24, 2014 at 07:04 AM
It's not a bug, or rather not a Unity bug; your code has several problems. First, the code won't do anything because you forgot a { after function Update (). If you'll notice, the script won't compile. Once you fix that, it sort of works, though you should replace GetKey with GetKeyDown, since otherwise it will spawn cubes every frame the key is down. Even with GetKeyDown, it's not ideal, since "tmp" is always a reference to the latest cube spawned, so destroying tmp only removes that reference, not any of the other cubes that may have been spawned.
Unrelated, you should remove references to UnityEngine, since that's automatically imported anyway. Also you can remove GameObject since Destroy inherits from Object. In other words, replace "UnityEngine.GameObject.Destroy" with just "Destroy". Also, you should remove the ; after your if statements. They don't hurt exactly, but they're not doing anything and could cause confusion.
Thanks for your responce. It's not actual code, just sample code to easy reproduce the bug. It has contained some errors, because it's just cutted from actual code, written for unity features testing, without any unrelated text.
The thing is, if I create primitive by function, and then destroy it as I do in X key block, its mesh is still on scene and in scene hierarchy window. If I replace GameObject.CreatePrimitive(PrimitiveType.Cube) to Instantiate(PrefabCubeVariable), where PrefabCubeVariable contains cube prefab, it's works properly and deletes all I need.
That's not what happens though. There is no (Unity) bug, as I mentioned. Once you fix the code you posted as I described, it works, as long as you only spawn one cube.
Ok, sorry, looks like you're right now. This problem actually was with Get$$anonymous$$ey. Thanks for help.
But it's still not solve the bug I tried to reproduce. When I create blocks and then them aren't delete. :( The code there looks like that:
function Start () {
groundBlocks = new GameObject[CHUN$$anonymous$$_SIZE * CHUN$$anonymous$$_SIZE];
//create plane ground TODO: noise;
for (var ix = 0; ix < CHUN$$anonymous$$_SIZE; ix ++)
for (var iy = 0; iy < CHUN$$anonymous$$_SIZE; iy ++) {
groundBlocks[ix * CHUN$$anonymous$$_SIZE + iy] = Instantiate(BlockPrefab);//GameObject.CreatePrimitive(PrimitiveType.Cube);
groundBlocks[ix * CHUN$$anonymous$$_SIZE + iy].transform.position.x = x * CHUN$$anonymous$$_SIZE + ix;
groundBlocks[ix * CHUN$$anonymous$$_SIZE + iy].transform.position.z = y * CHUN$$anonymous$$_SIZE + iy;
groundBlocks[ix * CHUN$$anonymous$$_SIZE + iy].GetComponent($$anonymous$$eshRenderer).material = Grass$$anonymous$$aterial;
}
}
function OnRemove() {
for (var gbi = 1; gbi < CHUN$$anonymous$$_SIZE * CHUN$$anonymous$$_SIZE; gbi++) {
UnityEngine.Object.Destroy(groundBlocks[gbi]);
}
UnityEngine.Object.Destroy(gameObject);
}
And, when I call OnRemove on that script, all blocks not disappearing from scene.
Okay, already solved this. The two errors is: 1) calling Start() manually 2) indexing from 1 in line 15
Your answer
Follow this Question
Related Questions
The first object in selection grid can't be activated. Why? 1 Answer
How to remove a word from string 0 Answers
Help Needed! expecting (, found 'Chop'. 1 Answer
Loading next level in js 1 Answer