Search Unity

Great unity tips but don't understand how one is supposed to work with script events.

Discussion in 'Editor & General Support' started by MongooseJV, Feb 5, 2017.

  1. MongooseJV

    MongooseJV

    Joined:
    Feb 25, 2014
    Posts:
    20
    So I read through 50 Tips for Working with Unity (Best Practices) found here: http://devmag.org.za/2012/07/12/50-tips-for-working-with-unity-best-practices/

    However I'm having an issue following this tip:

    Don’t put meshes at the roots of prefabs if you want to add other scripts. When you make the prefab from a mesh, first parent the mesh to an empty game object, and make that the root. Put scripts on the root, not on the mesh node. That way it is much easier to replace the mesh with another mesh without loosing any values that you set up in the inspector.

    Back to my issue. I created a prefab called GameBlockFab that looks like this in the hierarchy:

    GameBlockFab
    -> Cube

    Cube is a child game object under the root GameBlockFab. The Cube is the mesh just like the tip recommends. My Cube renders a cube with a rigid body & box collider. I have all of my scripted functionality for the fab in a script called GameBlockScr that is attached to the GameBlockFab (root). This is also what I believe is recommended above.

    Now for my issue. When the Cube collides or triggers with something else I do not receive any event on my script. OnTriggerEnter and OnCollisionEnter do not fire. I believe this is because there isn't a script attached to my Cube (where the rigidbody/collider components are). I want my parent script to receive the events... Is this possible? How can the author suggest this or what am I doing wrong?

    I do not have an issue trapping events when the script is attached to the same game object but in this case I want my script to be on a parent object...

    Thanks,
    - Jeffrey A Voigt
     
  2. Gizmoi

    Gizmoi

    Joined:
    Jan 9, 2013
    Posts:
    327
    Place the Rigidbody on GameBlockFab inside. The collision events should go to the 'attached' RigidBody, which IIRC is the first RigidBody found in the parent.
    However if you want Trigger events I think the script must be on the same object.
     
  3. MongooseJV

    MongooseJV

    Joined:
    Feb 25, 2014
    Posts:
    20
    So would you agree with the tip then? I'm not sure how triggers would be possible in this setup if it is as you say.
     
  4. Gizmoi

    Gizmoi

    Joined:
    Jan 9, 2013
    Posts:
    327
    I would agree it's a good idea to keep your behaviour separate from your models and even your models separate from your collisions. Its always worked out for me that for situations where I use triggers the script is located on the same object - buttons etc. Whereas when I build complex compound colliders under a parent object, they are for collision and go to the RigidBody.

    If you must have Trigger events directed elsewhere, I'd probably make a little helper script to throw on the collider that picked up the event and fired it out via a C# event Action/Delegate.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class TriggerReceiver : MonoBehaviour
    4. {
    5.     public event Action<Collider> Triggered = null;
    6.  
    7.     private void OnTriggerEnter(Collider other)
    8.     {
    9.         if (Triggered != null)
    10.         {
    11.             Triggered(other);
    12.         }
    13.     }
    14. }
    Something like that.
    Note the above code is untested.