Scratch To Unity: Events

Beginner Scratch Tutorial Unity

This post provides a handy reference guide to go from Scratch’s “Events” category of code blocks to C# with Unity.

Katori Itto Đô chỉ huy đồng tri
Wednesday September 09, 2020

Phim ảnh DC Extended Universe (DCEU). Thảo luận trong ‘Phim ảnh’ bắt đầu bởi yreimm0308n90, 4/5/15. Trang 2227 của 2227 trang

The post Katori Itto Đô chỉ huy đồng tri appeared first on Dua xe moto.

Code Blocks
Click on the picture of any of these Scratch code blocks to see how to write similar code statements in C#.

Game Con quay Spinner – Fidget Spinner Hero
Wednesday September 09, 2020

Chiến binh. Game Con quay Spinner – Fidget Spinner Hero. Trò chơi Con quay Spinner – game Con quay Spinner Con quay Spinner là trò chơi đang làm “điên đảo” giới trẻ trong thời gian gần đây nay đã xuất hiện trên Game vui để bạn có thể chơi trực tuyến ngay trên màn hình máy […]

The post Game Con quay Spinner – Fidget Spinner Hero appeared first on Dua xe moto.

Most of the time, you will probably use the “Start” method as an equivalent event. Technically speaking it is not the same. The “Start” method of a “MonoBehaviour” is called whenever the script itself is enabled. If you actually begin a scene with the GameObject or the script disabled, then the “Start” method will not be called until you enable them.

void Start()


XzeddyX ▶Ngự Miêu Vệ◀ Moderator
Thursday September 10, 2020

Cuộc sống Thư giãn Thư Giãn Express – Bản Tin Cuối Ngày [Zing] Sự bất an của người già châu Á. Thảo luận trong ”Thư Giãn Express – Bản Tin Cuối Ngày” bắt đầu bởi tieunhi.lang 9/9/20 lúc 21:25. Trang 3 của 3 trang

The post XzeddyX ▶Ngự Miêu Vệ◀ Moderator appeared first on Dua xe moto.

There is also a “SceneManager” which has a “sceneLoaded” event. Technically this might be more of an equivalent, but depending on how you register for the event, you could experience other challenges. For example, if you try to register in an “OnEnable” method, but your GameObject is disabled when the scene begins, then you will not be registered in time to receive the event. See more here.

Unity does not post any events for input such as key presses. Instead, you must poll for the event such as by using the “Update” method:

private void Update()
if (Input.GetKeyDown(KeyCode.Space))
// This is your event

The simplest way to detect a mouse click is to add the UI Button component to your Sprite. You can then add a reference to a call back method for its “onClick” event right from the Unity inspector. See here for more.
Make sure to include the using directive if you want to work with a Button in code:

using UnityEngine.UI;

You can also attach callbacks in code like this:

public Button button;

void Start()

void ButtonClicked()
Debug.Log("You have clicked the button!");

If for some reason you do not want to add the Button component, you can also register to receive pointer click events directly. To do this, make sure and include the necessary using directive:

using UnityEngine.EventSystems;

Mark your class as conforming to the “IPointerClickHandler” interface:

public class Example : MonoBehaviour, IPointerClickHandler

and then provide an implementation of the handler method:

public void OnPointerClick(PointerEventData pointerEventData)


If I wanted to observe an event based on changing the sprite, I would probably create my own method to change the sprite and have it post an event as needed.
You need to add the using directive for events:

using UnityEngine.Events;

Then implement something like this:

public UnityEvent backgroundChanged;

public void SetBackground(Sprite sprite)
var spriteRenderer = GetComponent<SpriteRenderer>();
spriteRenderer.sprite = sprite;

In Unity, there is no exposed “loudness” variable. You have “volume” on “AudioSource” components, but that is how loud to play the clip as a whole, not how loud is the sound that is actually being heard. In other words, imagine you have turned the volume knob of a speaker all the way up. When sound plays, it will be loud, but there may be moments between sounds where there is nothing to hear.
In order to analyze the loudness of what is actually playing, you can sample your audio data. Here is a post on the Unity Answers forum that discusses this topic in detail, complete with code.

The native equivalent to working with messages in Unity is to use custom events. You could define an event like this:

public UnityEvent messageEvent;

And then you could add observers from the Unity Inspector, or from code like this:

private void Start()

void OnMessage()


Assuming I have an event named “messageEvent”, I could “broadcast” to its observers like this:


I don’t have any recommendations for using the pattern in this code block. The closest thing might be to have a chain of observed events. One event on one script is broadcast, another script observes it and takes some action, then when the action is complete, the other script posts an event that the first script observed.
This should cover all of the code blocks in Scratch’s “Events” category. If you have any questions about this reference guide feel free to ask below.
If you find value in my blog, you can support its continued development by becoming my patron. Visit my Patreon page here. Thanks!