Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Cloned gameObjects with one Script

Discussion in 'Scripting' started by tmanallen, Nov 13, 2015.

  1. tmanallen

    tmanallen

    Joined:
    Nov 8, 2009
    Posts:
    395
    Hello all,
    simple question, I have a prefab that I am cloning and it has a script that when I hit the cloned object, it is suppose to explode, well it seems that when I hit one clone they all explode. I have done some googling and I may be phrasing the question wrong but would someone please point me in the right direction?


    Thank you.
     
  2. SeriousBusinessFace

    SeriousBusinessFace

    Joined:
    May 10, 2014
    Posts:
    127
    Without the script, it will be difficult to pin-point any problem. In the case that you can't post the script, are you using any static variables to determine if a clone should explode? As there is only one copy of a static variable, code like this will result in every clone exploding:
    Code (csharp):
    1.  
    2. static bool _shouldExplode = false;
    3.  
    4. void kill()
    5. {
    6.     _shouldExplode = true;
    7. }
    8.  
    9. void Update()
    10. {
    11.     if (_shouldExplode)
    12.         Destroy(gameObject);
    13. }
    14.  
     
  3. tmanallen

    tmanallen

    Joined:
    Nov 8, 2009
    Posts:
    395
    Thanks for the reply, but there is no script, because the issue is that I put a script on the main prefab that says if the raycast of the mouse hits the object destroys, when Instantiate that prefab all the game objects are destroyed that is what I am confused about but I did try this and still get nothing back.

    Bottle = GameObject.FindGameObjectsWithTag("Bottle");

    foreach (var bottle in Bottle)
    {
    print(bottle);
    }

    I get nothing back, the inspector is empty because I am calling the game objects with that tag.

    Any suggestions?
     
  4. tmanallen

    tmanallen

    Joined:
    Nov 8, 2009
    Posts:
    395
    Ok so I am seeing this doesn't work, GameObject.FindGameObjectsWithTag("Bottle") as GameObject[]; anyone has any ideas why?
     
  5. tmanallen

    tmanallen

    Joined:
    Nov 8, 2009
    Posts:
    395
    Ok maybe a moderator or someone from unity can help. I am attaching a script to the main game Object and I am cloning that object, when I press a the keyboard to destroy the cloned game object, it destroys all the clones and not just one, anyone has an idea that could help?