Scratch To Unity: Control

Beginner Scratch Tutorial Unity

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

Alumni Contributions to Oscar-Winning Films
Monday February 25, 2019

Six LAFS alumni worked on 4 Oscar-winning films at the 91st Academy Awards We are honored to announce the LAFS alumni who were credited on four of the Oscar-winning films including BlacKkKlansman, Black Panther, First Man and If Beale Street Could Talk. Their roles ranged from property master to drone safety technician to supervising art […]

The post Alumni Contributions to Oscar-Winning Films 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#.

What It’s Like To Write For A Network TV Show
Tuesday May 07, 2019

We chatted with the writer/creator of “F is For Family” and Co-producer of “The Simpsons,” Michael Price. If you can believe it, The Simpsons has been on Fox Television for 30 years and is the longest-running American sitcom, ever. Fox announced earlier this year that it has renewed the show for another two seasons, which […]

The post What It’s Like To Write For A Network TV Show appeared first on Dua xe moto.

IEnumerator Start()
{
// Optional code here
yield return new WaitForSeconds(1f);
// Optional code here
}

for (int i = 0; i < 10; ++i)
{
// Code to repeat
}

You can simply use the “Update” method of a “MonoBehaviour”:

Earth Day (Ahem, Earth Week)
Wednesday April 24, 2019

We’re doing a giveaway! Earth Day 2019 has passed, but we’re extending our earth-friendly efforts all week long. On Monday, we held an Earth Day event on campus to encourage the LAFS community to clean up Hollywood. Thank you to all of those who participated and showed their support for our planet. While there are […]

The post Earth Day (Ahem, Earth Week) appeared first on Dua xe moto.

void Update()
{
// Code to repeat per frame
}

Or, you can use a “while” loop. Make sure to provide some way to pause execution (like a yield statement in a Coroutine) or you will lock up Unity.

IEnumerator Start()
{
while(true)
{
// Optional code here
yield return null; // wait a frame
// Optional code here
}
}

// "condition" is anything that evaluates to true or false
if (condition)
{
// Code to execute when condition is true
}

// "condition" is anything that evaluates to true or false
if (condition)
{
// Code to execute when condition is true
}
else
{
// Code to execute when condition is false
}

// Optional code here
// "condition" is anything that evaluates to true or false
yield return new WaitUntil(() => condition);
// Optional code here

This is basically a “while” loop where you want the condition to be false:

// "condition" is anything that evaluates to true or false
while (!condition)
{
// Code to repeat
}

You can stop a script by disabling it.

enabled = false;

Or, if you were running a Coroutine you could stop it:

var coroutine = SomeCoroutine();
StartCoroutine(coroutine);
StopCoroutine(coroutine);

There isn’t a “good” equivalent for this, but a possible solution exists based on a pattern Unity has implemented. Whenever a clone is instantiated, the name of the clone will have “(Clone)” added to the end of its name. I only say it isn’t a “good” solution because you can rename an instantiated clone and then wouldn’t have any way to determine that status.

void Start()
{
if (name.Contains("Clone"))
{
Debug.Log("I'm a clone!");
}
}

Instantiate(gameObject);

Destroy(gameObject);

Summary
This should cover all of the code blocks in Scratch’s “Control” 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!