1

I an trying to destroy prefab1 below ( destroying its game object) in update but the prefabs do not get removed in my game. DESTROYING HOLE 1 GEMS debug below appears in console and gameobject is destroyed but the gems (prefab sprite) do not get removed from my game even when gemCount_Hole1 is less than 1 . Please help

            if (gemCount_Hole1 < 1)
        {
            Debug.Log("DESTROYING HOLE 1 GEMS");
            if (prefab != null) Destroy(prefab.gameObject);
        }
        else
        {
            for (int i = 0; i < gemCount_Hole1; i++)
            {
                Instantiate(prefab, new Vector3((i + xPos_Hole1) * 2.0F, -14, 0), Quaternion.identity);
            }
        }

I created a list but dont know how to write the destroy. Here is how I coded it as a list

 private System.Collections.Generic.List<GameObject> list ; 
 void Start()
 {
  list  = new System.Collections.Generic.List<GameObject>();
 } 
 void Update()
 {
  for (int i = 0; i < gemCount_Hole1; i++)
  {
   list .Add( Instantiate(prefab, new Vector3((i + xPos_Hole1) * 2.0F, -14, 0), Quaternion.identity)) ;
  }
 }
AZSWQ
  • 199
  • 5
  • 24
  • You could be just looking for `Destroy` .... https://docs.unity3d.com/ScriptReference/Object.Destroy.html – Fattie May 28 '17 at 16:38
  • you cannot call that list "list" :) you have to call it say "gems" or "holes" - whatever is relevant. – Fattie May 28 '17 at 16:39
  • you ABSOLUTELY MUST NOT to this inside "update". you're thinking of "start" – Fattie May 28 '17 at 16:40
  • Are you saying I should not instantiate prefabs in update? That makes sense but I need to check for user clicks and destroy gems. – AZSWQ May 28 '17 at 16:42
  • I have to load a default of 4 gems per bucket at game startup - I am doing this using update () as well. Then when a user clicks on bucket 2 for example, I need to make its count to 0 (remove all gems from it) and update that bucket with 0 gems and add gems to some other buckets. I can get remove gems and add logic etc right but cannot figure out how to update my game screen using update(). And this logic goes on each time user clicks on a bucket - all its gems gets removed. I want to do only when user clicks a bucket. Right now my instantiate happens in update() every frame... – AZSWQ May 28 '17 at 16:43
  • Please also note - What I am trying to destroy is a transform that I call prefab and not a game object. I cant use the gameobject destroy I think because it is not a game object ? – AZSWQ May 28 '17 at 16:48
  • `Destroy(gemCount_Hole1[index])` What's hard about that? – Programmer May 28 '17 at 16:50
  • Its hard because my gemCount_Hole1 is now zero (after a user clicks on hole 1, I update it to zero because all gems in it are now removed) so it wont process the --for-- loop. When the original instantiate is performed, gemCount_Hole1 is 4. Now its 0 so how can I loop through to destroy the 4 gems that were instantiated? Instead of looping through is there a way I can destroy all 4 gems that were instantiated ? And after Fattie's comments above I am also confused if I should do it in Update or Start. Well I need to perform updates based on userclicks so Start() wont make my updates I guess – AZSWQ May 28 '17 at 17:04
  • Hi @starazure, I'm sure everyone wants to help you, but you really have basic structural problems. just FWIW you *absolutely* should not use update *for any reason* - use "start" for this situation. even setting that aside, you have massive basic problems, beyond what can be addresses in a QA – Fattie May 28 '17 at 22:51
  • @Fattie - I got it to work - Its clear that you are here not to help but to try and demotivate. You have massive attitude problems and should not be in a help based forum !! – AZSWQ May 28 '17 at 23:34
  • hi @StarAzure, I have no idea what you are talking about. I think you are facing two problems, you can't use Unity keywords **prefab** and **update**. Good names for your "prefab" thing is "model", that's a standard thing to name that. And for your "update" call what about "refresh" or "setup". – Fattie May 29 '17 at 12:52
  • hi @StarAzure you may be facing a problem that you really need to use Unity's (incredibly simple) event system - it is basically the whole key to doing unity engineering. Fortunately it is particularly easy, it will likely solve all your problems here https://stackoverflow.com/a/36249404/294884 – Fattie May 29 '17 at 12:52
  • @Fattie - You are just writing comments and not anything to do with the question. My question was about destroying and updating. I have already solved that with the help of 1 sentence answer below instead of your lectures. You really should rethink about participating in forums where people ask questions to learn and not because they work at Google. And using keywords prefab works just fine - Did you try it out ? NO. So just shhh ! – AZSWQ May 29 '17 at 18:55
  • hi @StarAzure - I know what you mean, it *seems* that using "prefab" as a variable name can work, but honestly it causes problems later on. the usual thing to call it is "model". it's funny ... you've hit on a really big issue in Unity. Sometimes it is ***best to in fact not use*** prefabs, and rather, quite simply use a "model" of your dragon, tank, or whatever. You just keep the "model" offscreen, and `Instantiate` it as needs be. [this post](https://stackoverflow.com/a/36477040/294884) and many others discuss the issue. So you've really hit the nail on the head. – Fattie May 29 '17 at 19:28
  • however it's not possible to call it "prefab" for other reasons, usual is "model". (I have no clue what you are talking about Google, etc, sorry ??) – Fattie May 29 '17 at 19:30

1 Answers1

2

You can't destroy a Prefab directly.
When you instantiate the prefabs as GameObjects, you have to store them into an Array GameObject[] or List then, when destroying it, finding it in the list and destroy it from there.

byIcee
  • 145
  • 1
  • 14
  • I did that - See updated question. But I get error because I probably dont know how to destroy the transform prefab. Please take a look at my list code above and advise how I can destroy all instantiations of prefab transform – AZSWQ May 28 '17 at 16:36
  • 1
    @StarAzure You don't. It doesn't exist in the scene, it cannot be removed. You have to save a reference to the instantiated clone and destroy *that.* – Draco18s no longer trusts SE May 29 '17 at 03:04
  • I was able to store it to a list and destroy all in the list using a loop. Worked fine. Thanks – AZSWQ May 29 '17 at 03:18
  • 1
    "Worked fine. Thanks" If the problem is resolved, please accept the answer. – Alox May 29 '17 at 12:43