Game development

Stage 2 · Lesson 3 of 7

Components talking: references and C# events

Programming for games

scripts () talk to each other in three main ways. You can set a reference by dragging an object into a slot in the . You can call GetComponent to find another component on the same object.

Or you can use . A C# event or lets a component announce that something happened, such as health changing. Any number of listeners, such as the health bar and a sound player, can then react, and the sender does not need to know about them.

This keeps code decoupled (parts do not depend on each other), so you can add features without editing the player script. Subscribe to an event in OnEnable and unsubscribe in OnDisable. The next stage relies on this, when UI, audio and gameplay all react to the same events.

Common trapcalling GetComponent or Find every frame instead of looking up references once and storing them.

A question to check yourself, an AI practice session and 2 cheat sheet entries come with the full roadmap. Unlock

Resources for this stage

Start with· follow one of these

  • CourseFree
    Unity Learn: Junior Programmer Pathway

    Unity Technologies · Beginner

    Unity's official free C# pathway, with small playable projects in every unit

  • GuideFree
    Learn C#

    Microsoft · Beginner

    Microsoft's free C# lessons, for the language itself when Unity code feels unfamiliar

Practise

  • Free mentored exercises in C# and C++ to strengthen the language outside the engine

Keep handy

Also worth your time

Mark this stage as not done?