- Home /
Deleting a GameObject
This question seem to get asked a lot, which makes it slightly more embarrassing that I can't get it to work!
I am trying to delete an Instantiated prefab that a user selects, I've checked through loads of questions on this very subject, but using Destroy doesn't seem to work :/
//class level variable to store the object the user has selected
private GameObject selectedItem;
...
public void OnGUI (){
//if delete button is pressed
if (GUI.Button(...))
{
Destroy (selectedItem);
}
}
...
//use Raycast to detect the GameObject the user has touched
//this is called from within Update() stack
if (Physics.Raycast(cursorRay, out hit, Mathf.Infinity, this.layerMask)){
switch (hit.transform.tag) {
case "tag":
selectedItem = hit.transform.gameObject;
break;
As far as I can tell, it sets the GameObject fine (it changes its color in other parts of the code), but the Destroy will not get rid of it!
Has anyone got any ideas? I know this isn't much code to go by, but if you need more info/code let me know!
Cheers,
Dave McB
that's because your GameObject is private
how are you referencing witch gameobject is this: selectedItem
as all you said how you reference it is:
...
not much info right, ...
well anyway I suggest you make it public and drag GO from inspector in to the script ;)
allso stripping code down as much as possible is gr8 to get good answers so no unwanted garbage is in the way of problem but everything that does change or does have effect on your question does need to be inside the code.
like if you'd skip :
private GameObject selectedItem;
and you say in your code
prvate GameObject selectedItem;
so make an typo and not posting it is like chasing shadows in the dark for us
Yeah, the whole make it easier to understand is a bit of a catch 22 :(
Dragging the GO onto the script doesn't seem to make the item appear in the Heirarchy, but the Destroy function now throws an error saying:
Destroying assets is not permitted to avoid data loss.
If you really want to remove an asset use DestroyImmediate (theObject, true);
However DestroyImmeadiate seems to get rid of every single instance of that prefab (not what I want)
Answer by MaT227 · Jun 12, 2013 at 06:16 AM
Like sdgd, I would say, first make your GO public so you can check in the inspector if it's well set and if it corresponds to the right GO in the scene. Next you can try other function like DestroyImmediate. You can also check if there is no error in the debug.
Answer by lighting · Jun 12, 2013 at 06:39 AM
The cause that came up to my mind is that this GO is actually being destroyed but another Raycast (because it's being called in Update()) assigns this GO again and again (line 21 in your code). Please, check also if in other places in your code, you don't reinitialize this GO.
Your answer
Follow this Question
Related Questions
Instantiating prefabs: "The object of type GameObject has been destroyed". 1 Answer
Accessing children of an instance doesn't work every time 1 Answer
Trouble with destroying an instantiated prefab 2 Answers
Assign an instantiated GameObject? 1 Answer
Check if object is destroyed on level load, if so instantiate prefab? 1 Answer