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. Dismiss Notice

OnDisable with Instantiating Prefabs

Discussion in 'Scripting' started by mquickel, Jun 27, 2013.

  1. mquickel

    mquickel

    Joined:
    Jun 21, 2013
    Posts:
    11
    This doesn't seem to fire for me:

    Script "A" gets a prefab passed to it and create 20 instances of it through a loop:
    Code (csharp):
    1. var o = GameObject.Instantiate(_antibody, new Vector3(Random.Range(-10, 10), 0, Random.Range(-10,10)), Quaternion.identity);
    Script "A" has OnDisable() specified:
    Code (csharp):
    1.     void OnDisable()
    2.     {
    3.         Debug.Log ("Destroyed");
    4.     }
    Script "B" destroys the item:
    Code (csharp):
    1.     void OnControllerColliderHit(ControllerColliderHit hit){
    2.         Destroy(hit.gameObject);
    3.     }
    I appreciate any help! Thanks!
     
  2. Zibber

    Zibber

    Joined:
    Jun 22, 2013
    Posts:
    86
    Does script B destroy the newly instantiated prefabs? Or does it destroy the object creating the prefabs?
     
  3. mquickel

    mquickel

    Joined:
    Jun 21, 2013
    Posts:
    11
    It destroys the prefabs that are instantiated. I am unsure how to tie the event to the instantiated prefabs vs the script...(new to Unity)
     
  4. cdevl

    cdevl

    Joined:
    Apr 10, 2013
    Posts:
    180
    Looks like your OnDisable() is attached to the A script that instantiates from prefabs. You are not destroying script A (based on above) - you are destroying the object instantiated from prefab. So attach it to that prefab. Also it should not be OnDisable() - it should be OnDestroy().
     
  5. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,514
    OnDisable() is valid, and I'm pretty sure it gets called upon destroying an object since it implicitly gets disabled along the way.
     
  6. cdevl

    cdevl

    Joined:
    Apr 10, 2013
    Posts:
    180
    Well, it is purely a question of intent. Does the functionality between destroy and disable need to be shared. If yes then you are right. Otherwise, it is just an unclean way of doing things.

    I agree though, I should have state that that part was optional.
     
  7. mquickel

    mquickel

    Joined:
    Jun 21, 2013
    Posts:
    11
    I do get the difference between OnDisable and OnDestroy. So outside of creating a separate script and attaching it to the prefab, is there any way for me to tie an event to the objects called from Instantiate? Since "o" is the output of the object being created, can I do something like o_OnDestroy += (I know this doesn't work, but I am looking for a way to bind an event like I would in normal C#).

    Thanks.
     
  8. cdevl

    cdevl

    Joined:
    Apr 10, 2013
    Posts:
    180
    As far as I know, creating a separate script and attaching it to prefab is the only way to go. Off course, you can attach it to prefab from your code dynamically by using AddComponent as well.