Delegates and Events , prime31 tutorial...

Hi
In this video prime31 is showing how to use delegates :

I like it , and I use it all the time. Thank you Prime31.

I have question : In this video event listener is attached to couple different cubes. When I press a button all objects are doing exactly the same cool… is there a way to specify in event listener script with cube should move , after buttons is pressed ?
Imagine that you are touching one of this cubes and you want it to jump but only this that you touched .

Or the only solution is to have 3 different event listeners even if they are exactly the same ?
I was trying using tags in event handler but it doesn’t work.

Thank you in advance

Hi AngryOrange

The point of events is to have multiple listeners, so in your use case they might not be the way to go. You could send your event with a cubeID param, that each cube would then check against its ID to react or not, but that would be a very ineffective way of doing things. In this case, I’d advise a simple SendMessage to the touched cube.

I didn’t see this movie, but if you want to handle that through delegate you can do something like this
It’s a pserudo code

public delegate CubePress(GameObject obj);

class blabla {
    public static CubePress onCubePress;

    void Init(){
        onCubePress+=onCubePressListener();
    }

    void onCubePressListener(GameObject obj){
        if (obj==this){
            jump();
        }
    }

    void onTriggerEnter(){
        if (onCubePress!=null){
            onCubePress(this);
        }
    }
}

thank you grezo and nicloay this is exactly what I was thinking about… First don’t use delegates but simple SendMessage and second send a GameObject as a param… I will try this today.
I thought maybe is something like “only delegates trick” to do this but it sounds like I’am on a good track.
Thank you guys :slight_smile: