- Home /
My prefab isn't getting destroyed
Rundown of my problem:-
public void drop() {
//creates a bobber
bobber =(GameObject) Instantiate(bobberPrefab, new Vector3(-0.999f, 0.168f, 0), new Quaternion(0f, 0f, 0f, 0f));
}
public void restart()
{
Destroy(bobber.gameObject);
}
So what you need to know is that method "drop" gets called, prefab does it's thing, then calls method "restart" and supposedly gets destroyed
Now here is the problem, The prefab isn't getting destroyed at all, it just sits there, I know that the method does get called because it used to give me errors for destroying the asset, does anyone see what's wrong? thanks.
(also I have set all references)
Nothing wrong with the code as is, will probably have to see some more to get a better idea of what is happening. Likely bobber
isn't pointing at what you thing it's pointing at. Do you only spawn 1 bobber or multiple?
Are you sure Restart
is being called? check by putting a Debug message in there.
Could I see the rest of the code and get a little more description of what is going on? Like does Drop get called more than once?
Drop gets called only once (only need 1 bobber)
bobber
is a GameObject itself and it's a bit weird that you are trying to access a GameObject Component of a GameObject, i.e. bobber.gameObject
.
I'm not sure that that's what's causing the problem, but you have nothing to lose by trying Destroy(bobber);
I tried only bobber, it still doesn’t work.
So, the problem is in another part of your project that you have not posted.
As others have suggested the problem lies elsewhere in your code, use debugging in your restart function to ensure this is being called. If this doesn't help you then you'll need to show us where restart is being called.
Answer by LOSTSOUL86 · Jan 05, 2020 at 10:36 AM
Did you try to write:
Destroy(bobber);
Also maybe put:
Debug.LogError("Restart called");
to see if its really getting called. Both functions are in the same class? And bobber is global variable? Also do you instantiate only one bobber game object or more? Do you assign anything else to bobber variable after instantiating game object?