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

Prefab wont destroy on collision

Discussion in 'Scripting' started by Jorjor210, Jun 9, 2015.

  1. Jorjor210

    Jorjor210

    Joined:
    May 3, 2015
    Posts:
    91
    I'm currently making a script that allows a object to be destroyed on trigger, and it works fine, but when I make the object re spawn the the on trigger enter does not work? It's a 2D game and I've mad similar scripts for 3D game with success, but for some reason the script seem not to work in 2D? and yes I did OnTriggerEnter2D ( Collider2D info ). I feel like it might have to do with layers, but Idk 100%. any ideas?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    It works on the one in the scene, but not when spawned from a prefab, is that right? Maybe there's some setting or component on the in-scene one that hasn't been applied back to the prefab?
     
  3. Jorjor210

    Jorjor210

    Joined:
    May 3, 2015
    Posts:
    91
    "t works on the one in the scene, but not when spawned from a prefab, is that right?" yes that is correct. but "Maybe there's some setting or component on the in-scene one that hasn't been applied back to the prefab?" I thought of that solution, so i dragged the prefab onto the scene and I could collect it like the regular object, but again. after I collect it and a new one spawns the newly spawned object I cant collect..
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    That's really strange. Is the other object (the one that collects these objects) doing some initialization where it finds all the objects available in the start of the scene?

    Also, is there anything in the console?
     
  5. Jorjor210

    Jorjor210

    Joined:
    May 3, 2015
    Posts:
    91
    Nope, the player has no scripts attached that allows him to "find all the objects available in the start" it's just a simple "OnTriggerEnter" script. attached to the prefab like so: (not all of code but most. the rest is irrelevant )
    Code (CSharp):
    1. void  OnTriggerEnter2D ( Collider2D info  ){
    2.  
    3.         if (info.name == "Player")
    4.         {
    5.             Debug.Log ("Hitting");
    6.             Num = Num+1;
    7.             Score.score += +1;
    8.             FinalScore.score1 +=+1;
    9.             Destroy(gameObject);
    10.  
    as for the spawning script:

    Code (CSharp):
    1. void OnTriggerEnter2D (Collider2D info)
    2.     {
    3.         if (info.name == "Player")
    4.         {
    5.             SpawnCoin();
    6.         }
    7.     }
    8.    
    9.     void SpawnCoin()
    10.     {
    11.         Vector3 position = new Vector3(Random.Range(-9f, 9F), Random.Range(-5.56F, 5.27F), 0);
    12.         Instantiate (prefab, position, Quaternion.identity);
    13.     }
     
  6. Jorjor210

    Jorjor210

    Joined:
    May 3, 2015
    Posts:
    91
    I think I found the problem. For some reason beyond my knowledge the object spawns with all attached scripts on it, but the scripts are disabled?? (including Colliders )
     
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Put some Debug.Log's in OnTriggerEnter2D - one inside, and one outside that 'if' statement. Check to see if it's calling the trigger enter function, and if that if statement is just starting to return false.
     
  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    That shouldn't happen, as far as I know. Can you paste the code where the new thing gets spawned?
     
  9. Jorjor210

    Jorjor210

    Joined:
    May 3, 2015
    Posts:
    91
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class random : MonoBehaviour
    5. {
    6.     public GameObject prefab;
    7.  
    8.     void OnTriggerEnter2D (Collider2D info)
    9.     {
    10.         if (info.name == "Player")
    11.         {
    12.             SpawnCoin();
    13.         }
    14.     }
    15.    
    16.     void SpawnCoin()
    17.     {
    18.         Vector3 position = new Vector3(Random.Range(-9f, 9F), Random.Range(-5.56F, 5.27F), 0);
    19.         Instantiate (prefab, position, Quaternion.identity);
    20.     }
    21. }
    exact coding of re spawning object