Explore 100's Of Free Video Tutorials

Keep Learning, Keep Coding & Keep Sharing

else if ladder statement, game examples

Notes:

else if ladder statement, game examples:
I. Finding the largest of 3 numbers : [Starts at: 01min:23sec]
II. Controlling enemy state using ifs: [Starts at: 16min:50sec]

I. Finding the largest of 3 numbers : [Starts at: 01min:23sec]
Finding the largest of 3 enemy's health

Example Code:
using UnityEngine;
public class ConditionalStatements : MonoBehaviour
{
void Start ()
{
int enemy1Health = 40, enemy2Health = 85, enemy3Health = 90;
if ((enemy1Health > enemy2Health) && (enemy1Health > enemy3Health))
{
Debug.Log (enemy1Health + " is largest");
}
else if (enemy2Health > enemy3Health)
{
Debug.Log (enemy2Health + " is largest");
}
else
{
Debug.Log (enemy3Health + " is largest");
}
}
}

II. Controlling enemy state using ifs: [Starts at: 16min:50sec]

Example Code:
using UnityEngine;
public class ConditionalStatements : MonoBehaviour
{
void Start ()
{

bool collided = false;
float alpha = 100f;

Debug.Log ("Before bullet hit alpha= " + alpha); // 100

// if enemy hit by bullet
collided = true;

if ((collided == true) && (alpha >= 100f))
{
alpha = alpha * 0.5f;
collided = false;
}
Debug.Log ("After one bullet hit alpha= " + alpha); // 50

if (alpha == 50f)
{
Debug.Log ("Enemy in danger");
}
}
}