<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sadia Younas on Medium]]></title>
        <description><![CDATA[Stories by Sadia Younas on Medium]]></description>
        <link>https://medium.com/@sadiadev0001?source=rss-c08bf803699c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*NsuQjbjmsO-jgyZ4_Im7Ag.jpeg</url>
            <title>Stories by Sadia Younas on Medium</title>
            <link>https://medium.com/@sadiadev0001?source=rss-c08bf803699c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 12:20:13 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sadiadev0001/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Delegates-Events-Actions-UnityEvents]]></title>
            <link>https://medium.com/@sadiadev0001/delegates-events-actions-unityevents-f55f297f9ec7?source=rss-c08bf803699c------2</link>
            <guid isPermaLink="false">https://medium.com/p/f55f297f9ec7</guid>
            <dc:creator><![CDATA[Sadia Younas]]></dc:creator>
            <pubDate>Sat, 28 Sep 2024 17:26:41 GMT</pubDate>
            <atom:updated>2024-09-28T20:27:29.251Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Delegates</strong></h3><ul><li>Function Container</li><li>Allow you to store and call a function as if it were a variable</li><li>Trying to trigger an empty delegate will cause a null reference error</li></ul><p>There are two types of delegates in C#:</p><ol><li><strong>SingleCast Delegates</strong> trigger only one function at a time</li><li><strong>Multicast Delegates</strong> instead of triggering only one function, they can be used to trigger multiple functions all at once.</li></ol><p><strong>Example: Single Cast Delegate</strong></p><pre>    delegate void MyDelegate();<br>    MyDelegate attack;<br><br>    private void Update()<br>    {<br>        if(Input.GetKeyDown(KeyCode.A))<br>        {<br>            attack = Attack_A_Fun;<br>            attack?.Invoke();<br>        }<br>        if (Input.GetKeyDown(KeyCode.B))<br>        {<br>            attack = Attack_B_Fun;<br>            attack?.Invoke();<br>        }<br>    }<br>    void Attack_A_Fun()<br>    {<br>        Debug.Log(&quot;A Attack&quot;);<br>    }<br>    void Attack_B_Fun()<br>    {<br>        Debug.Log(&quot;B Attack&quot;);<br>    }</pre><p><strong>Example: MultiCast Delegate</strong></p><pre>delegate void MyDelegate();<br>    MyDelegate attack;<br><br>    private void Start()<br>    {<br>            attack += Attack_One_Fun;<br>            attack += Attack_Two_Fun;<br>            attack?.Invoke();  <br>    }<br>    void Attack_One_Fun()<br>    {<br>        Debug.Log(&quot;1 Attack&quot;);<br>    }<br>    void Attack_Two_Fun()<br>    {<br>        Debug.Log(&quot;2 Attack&quot;);<br>    }</pre><p><strong>Example: Delegates With Parameter</strong></p><pre>    public delegate int MultiplierDelegate(int num);<br>    MultiplierDelegate myDelegate;<br><br>    private void Awake()<br>    {<br>        myDelegate = MultipleByTwo;<br>        Debug.Log(myDelegate(2));<br>    }<br><br>    int MultipleByTwo(int num)<br>    {<br>        return num * 2;<br>    }</pre><p><strong>Example: Connecting two Scripts Using Delegate</strong></p><p>PlayerHealth.cs</p><pre>   public delegate void OnGameOver();<br>   public static  OnGameOver onGameOver;<br> //  In order for other scripts to be able to easily access the delegate, <br>// it needs to be public and it will need to be static<br><br>  float health = 0;<br><br>    private void Start()<br>    {<br>        Invoke(nameof(HealthCheck), 0.1f);<br>    }<br><br>     void HealthCheck()<br>    {<br>        if(health==0)<br>        {<br>            onGameOver?.Invoke();<br>        }<br>    }</pre><p>GameController.cs</p><pre>    private void Awake()<br>    {<br>PlayerHealth.onGameOver += RestartGame;<br><br>  //  PlayerHealth.onGameOver = null;<br> //One Problem here, Because any other script can access the delegate,<br>//its possible for any other script to call it or even clear the assigned<br>// functions from the delegate<br>    }<br><br>    private void OnDisable()<br>    {<br>        PlayerHealth.onGameOver -= RestartGame;<br>    }<br><br>    private void RestartGame()<br>    {<br>        Debug.Log(&quot;Restart Game&quot;);<br>    }</pre><h3>Events</h3><ul><li>Works in the same way as regular delegates</li><li>Events adds a layer of abstraction and protection on delegates</li><li>The ‘=’ operator can’t be used with events.</li></ul><p><strong>Example: Connecting two Scripts Using Events</strong></p><p>E_PlayerHealth.cs</p><pre>   public delegate void OnGameOver();<br>   public static event OnGameOver onGameOver;<br><br>    float health = 0;<br><br>    private void Start()<br>    {<br>        Invoke(nameof(HealthCheck), 0.1f);<br>    }<br>   <br>     void HealthCheck()<br>    {<br>        if(health==0)<br>        {<br>            onGameOver?.Invoke();<br>        }<br>    }</pre><p>E_GameController.cs</p><pre>     private void Awake()<br>    {<br>      E_PlayerHealth.onGameOver += RestartGame;<br>      //  E_PlayerHealth.onGameOver = null;<br>      // can&#39;t assign null when using events<br>    }<br>    private void OnDisable()<br>    {<br>        E_PlayerHealth.onGameOver -= RestartGame;<br>    }<br>    private void RestartGame()<br>    {<br>        Debug.Log(&quot;Restart Game&quot;);<br>    }</pre><h3>Actions</h3><ul><li>Ready made delegates</li><li>We can use actions in the same way as delegates, but the difference is that we don’t need to declare it first.</li></ul><p>Instead of defining a delegate and creating an instance of it, create an action that will work in the same way.</p><pre>public static event Action OnGameOver;</pre><p>Or</p><pre>public delegate void  OnGameOver();<br>OnGameOver onGameOver;</pre><p><strong>Example: Connecting two Scripts Using Actions</strong></p><p>A_PlayerHealth.cs</p><pre>  public static event Action OnGameOver;<br>  float health = 0;<br><br>    private void Start()<br>    {<br>        Invoke(nameof(HealthCheck), 0.1f);<br>    }<br>   <br>     void HealthCheck()<br>    {<br>        if(health==0)<br>        {<br>            OnGameOver?.Invoke();<br>        }<br>    }</pre><p>A_GameController.cs</p><pre>   private void Awake()<br>    {<br>        A_PlayerHealth.OnGameOver += RestartGame;<br>    }<br>    private void OnDisable()<br>    {<br>        A_PlayerHealth.OnGameOver -= RestartGame;<br>    }<br>    private void RestartGame()<br>    {<br>        Debug.Log(&quot;Restart Game&quot;);<br>    }</pre><h3><strong>Actions With Parameter</strong></h3><p>A_PlayerHealth_Parameter.cs</p><pre> public static event Action&lt;string&gt; OnGameOver;<br> float health = 0;<br><br>    private void Start()<br>    {<br>        Invoke(nameof(HealthCheck), 0.1f);<br>    }<br>   <br>     void HealthCheck()<br>    {<br>        if(health==0)<br>        {<br>            OnGameOver?.Invoke(&quot;Health Down&quot;);<br>        }<br>    }</pre><p>A_GameController_Parameter.cs</p><pre>   private void Awake()<br>    {<br>        A_PlayerHealth_Parameter.OnGameOver += RestartGame;<br>    }<br>    private void OnDisable()<br>    {<br>        A_PlayerHealth_Parameter.OnGameOver -= RestartGame;<br>    }<br>    private void RestartGame(string s)<br>    {<br>        Debug.Log(s);<br>    }</pre><h3><strong>Unity Events</strong></h3><p>UnityEventsLearn.cs</p><pre>   public UnityEvent gameStartEvent;<br><br>    private void Awake()<br>    {<br>        gameStartEvent?.Invoke();<br>    }<br><br>    public void StartGame()<br>    {<br>        Debug.Log(&quot;Game Start&quot;);<br>    }<br><br>    public void SetUI()<br>    {<br>        Debug.Log(&quot;UI Set&quot;);<br>    }</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/944/0*b55Vw8BWa8p7UhYb" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f55f297f9ec7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>