Hey, a noob question here. What is return and what does it do? Examples and explain in the way that easy for me to understand, please.
“return;” <— This.
Thanks a lot!!
Hey, a noob question here. What is return and what does it do? Examples and explain in the way that easy for me to understand, please.
“return;” <— This.
Thanks a lot!!
When you call ‘return’ in a function, the rest of the code will not get run.
public void Update()
{
//Code here will always run
Camera.main.transform.RotateAround(transform.position, Vector3.up, 0.5f);
if (Time.timeScale == 0)
{
return;
}
//This code won't get run when paused
transform.position += Vector3.right;
audioSource.PlayOneShot(footstepSound);
}
It is useful when you don’t want to do some stuff in certain scenarios, such as when the game is paused.
It also must be used in functions where a value is outputted. In those cases, you return whatever value you want outputted.
public float CalculateDamage()
{
if (Player.IsStunned)
{
return 0;
}
if(UnityEngine.Random.value < Player.CritPercentage)
{
return Player.BaseDamage * 2;
}
return Player.BaseDamage;
}
Hope this helps!