I need some help understanding events in C# please

Hello I am trying to learn how to use events in C# I have watched a couple tutorials and I see them create the delegate and the event and call the event all in the same script. Then have a second script that listens and and responds when the event is triggered.

What I want to know is how can I call events in any script and make the other scripts react. Like I have a script that starts the game which I want to call the gameStart(true); event. But when the ball ends I want to call the gameStart(false); event. I have tried doing this buy just calling the events in the scripts but it only listens to it inside that script and all the others ignore it. What am I missing to do this?

Events can be called only from class that publish them. You can make a method for calling event from another class, but I think it is wrong way. You should think about events like a reaction for some changes inside obect - when object chenged - it raise an event.

Ok then what would be the best way to do a major change across the game globally. Like a a game starting letting all objects scripts know that they can move and attack and power ups can start activating?

This tutorial explains how to let objects respond to an event.

One of tutorials I already watched. I am trying to find a way to call events outside of the script that makes them so I can make changes across many objects easily.

You can still call the events from another script as long as you have a reference to them.

The guy above you said that he thought that doing it like that was the wrong way. But it works for me so thanks. :slight_smile:

Here is an article about Event in Unity http://unitygems.com/getcomponent-vs-event/

You can’t invoke an event from another class. You can call a method though, and that method could trigger a specific event defined for that object.

Oh boolean arguments, how I hate thee. gameStart(false) is a pretty awful method signature. Are you starting the game or not? What does that false argument mean? :slight_smile:

Sorry for off-topic.

1 Like

Yea i know its pretty lazy and not well worded. Im just used it real quick for learning. It will eventually get passed a string so the game can have many different states rather than go and stop. Also changing that to gameStatus

That is what im doing now. And it is working well. And thanks for the article it helped me understand a little more.

Well you’re probably not using an “event” as defined by C#. More likely you’re calling a method. An event is not the same thing, even if your method execute a game event logic.

@ardizzle have a look into Actions and Func as well , very handy C# delegates

What exactly is happening right now is I set up an event script and made an event that passes asks for a boolean value. And then I set up a function in that same script that asks for a boolean value. Then every script that needs to call the event has a reference to the event script then calls the function while passing a boolean which in turn calls the event using the boolean I passed. After that every script that is listening to the event does what it is suppose to do.

Will do. Thanks.