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

instantiate to children?

Discussion in 'Scripting' started by tattyswad, May 22, 2012.

  1. tattyswad

    tattyswad

    Joined:
    Sep 22, 2010
    Posts:
    106
    Hi,

    I have the following code that spawns an object at a location when a trigger is entered. I also want to destroy the object when the trigger is left. At the moment the code doesnt work because the prefab is an FBX and gets called FBX(clone) when instantiated in the scene. How would I destroy the object? I'm thinking make the object children of the original trigger when instantiated but I'm not sure how.

    Any ideas?

    Thanks

    Code (csharp):
    1. var prefab : GameObject;
    2.  
    3. function OnTriggerEnter(myEnter:Collider){
    4.  
    5.     if (myEnter.gameObject.name == "MainCam"){
    6.     var spawn : GameObject = Instantiate(prefab, transform.position, transform.rotation);
    7.     Debug.Log("triggered");
    8.     }
    9. }
    10.     function OnTriggerExit(myExit:Collider){
    11.  
    12.     if (myExit.gameObject.name == "MainCam"){
    13.     Destroy(prefab);
    14.     Debug.Log("untriggered");
    15.     }
    16.  
    17. }
     
  2. TheChronicPhenix

    TheChronicPhenix

    Joined:
    Jan 14, 2010
    Posts:
    874
    You have it all setup to work, just destroy the gameObject "spawn" instead of "prefab".
     
  3. kingcharizard

    kingcharizard

    Joined:
    Jun 30, 2011
    Posts:
    1,137
    Code (csharp):
    1.     var prefab : GameObject;
    2.      
    3.     function OnTriggerEnter(myEnter:Collider){
    4.      
    5.         if (myEnter.gameObject.name == "MainCam"){
    6.         var spawn : GameObject = Instantiate(prefab, transform.position, transform.rotation);
    7.         Debug.Log("triggered");
    8.         }
    9.     }
    10.         function OnTriggerExit(myExit:Collider){
    11.      
    12.         if (myExit.gameObject.name == "MainCam"){
    13.         Destroy(spawn);
    14.         Debug.Log("untriggered");
    15.         }
    16.      
    17.     }
    if you wanted to make a child gameObject and have the prefab instantiate at it you can rewrite the code like this.

    Code (csharp):
    1. var prefab : GameObject;
    2. var targetSpawn : GameObject;  //This is where the child game object should be placed
    3.      
    4.     function OnTriggerEnter(myEnter:Collider){
    5.      
    6.         if (myEnter.gameObject.name == "MainCam"){
    7.         var spawn : GameObject = Instantiate(prefab, targetSpawn.transform.position, targetSpawn.transform.rotation);
    8.         Debug.Log("triggered");
    9.         }
    10.     }
    11.         function OnTriggerExit(myExit:Collider){
    12.      
    13.         if (myExit.gameObject.name == "MainCam"){
    14.         Destroy(spawn);
    15.         Debug.Log("untriggered");
    16.         }
    17.      
    18.     }
    The above code should work. I did not test it...

    also I don't recommend using prefab as a variable, I suggest you call it something else more to suit what it is, such as bullet, projectile or enemy if it is an enemy...
     
    Last edited: May 22, 2012
  4. tattyswad

    tattyswad

    Joined:
    Sep 22, 2010
    Posts:
    106
    Thanks,

    I think I will change the name of the variable.

    If i change the code to

    Destroy (spawn);

    (which I assume would be the easier option), it says unknown identifier 'spawn'. Is this because the variable 'spawn' was declared inside another set of squiggly brackets? How would I work around this?

    Also I tried the (targetSpawn.transform.position) approach and it didn't put the instantiated object into the heirarchy of the targetSpawn. :(

    Cheers
     
    Last edited: May 22, 2012
  5. tattyswad

    tattyswad

    Joined:
    Sep 22, 2010
    Posts:
    106
    Sorted it.

    i was missing the var spawn outside the function bit.

    Code (csharp):
    1. var buildings : GameObject;
    2.  
    3. private var spawn : GameObject;
    4.  
    5. function OnTriggerEnter(myEnter:Collider){
    6.  
    7.     if (myEnter.gameObject.name == "MainCam"){
    8.     spawn = Instantiate(buildings, transform.position, transform.rotation);
    9.     Debug.Log("triggered");
    10.     }
    11. }
    12.  
    13. function OnTriggerExit(myExit:Collider){
    14.  
    15.     if (myExit.gameObject.name == "MainCam"){
    16.     Destroy (spawn);
    17.     Debug.Log("untriggered");
    18.     }
    19.  
    20. }
    Thanks