- Home /
How Can i delete Object Clone
Hi Everyone, I am making one ball game and i am trying to create obstacle's in a dynamic way, I am using Instantiate for creating obstacles but when i am trying to Destroy my old object (clone) on that time i am facing some problems. If anyone know the solution please give reply....Thank You
How do you expect people to help you if you don't explain what the problem is?
mattssonon does have a point. In general, the more detail you can give on a problem, the better. Saying "I'm having problems" is not actually very helpful.
We really shouldn't post answers for questions like this, we're only encouraging bad questions.
$$anonymous$$ake the call Destroy(gameObject) on the actual object. Can't go wrong.
Answer by Hoeloe · Aug 30, 2013 at 09:21 AM
If you are destroying the object from within it's own script, be sure to call Destroy on the GameObject (`GameObject.Destroy(this.gameObject);`, otherwise you're only destroying specific components. If you're trying to destroy it from outside it's own script, then you need to keep a reference to it when you instantiate it, which you can then call Destroy on (for example, use: o = Instantiate(...);
followed by Destroy(o)
, where o
is a GameObject variable).
I am Using this Code but the problem is $$anonymous$$y prefab will also destroy after Destroy() I want to create Obstacles again and again in forward direction
IEnumerator Start() { yield return new WaitForSeconds(1.2f); create_instan(); }
void create_instan()
{
for(float y = Y; y < Y + 5 ; y++)
{
for(float x = 0 ; x < 3 ; x++)
{
GameObject bricks =(GameObject) Instantiate(brick,new V ector3(x,y,0),brick.transform.rotation);
x = x + 1;
// bricks.GetComponent<BallTouch>().brick = GetComponent<BallTouch>().bricks;
Destroy(bricks,0.5f);
}
}
}
...That won't destroy your prefab. Your prefab is called "brick", and you're destroying "bricks". What you are destroying is the specific instance of the prefab that you've just created, which you've called "bricks", not the prefab itself.
What that code will do is create an object and immediately destroy it, so at best you'll see the objects "flash" into existence, and then disappear.
prefab cannot be destroyed... may be you are trying to say is all the instantiated gameobjects are destroyed...
Also, I don't know what you think you're doing, but you're not using a for loop correctly. First of all, you should use "int", not "float" for your for loops, and secondly, you're already incrementing x in the loop (that's what the "x++" does), but you're also doing it again later ("x = x + 1"), which means that x will increment by 2 each time. If you wanted that to happen, use "x+=2" in the loop, don't use 2 separate clauses.
Your answer
Follow this Question
Related Questions
clicking the right mouse button and add an object 1 Answer
Positioning Objects Next to Each Other in Code 0 Answers
(New UI) 3D objects as buttons? 1 Answer
Destroy Object On Touch? 2D 1 Answer
Orbiting objects appear stretched 1 Answer