Hi All,
I’m enjoying this course. I realize there can be multiple answers/approaches to the same problem. This was my approach to the course. I essentially just created a Function to handle the level/scene loading and reloading instead. I heard what Rick said about having menus for scenes; however, I suspect that I would have an entirely separate script that would be checking for a Key Press or a special condition to be met.
I also would anticipate that any additional menu or other non-playable level/scenes I would probably arrange for those to be the first or last scenes of the scenes in build, and simply account for those in the index by setting a constant and utilize basic arithmetic to account for the max levels.
Acknowledging my limited experience regarding game design and programming, what issues can I have with this approach in the future?
public class CollisionHandler : MonoBehaviour
{
const string FINISHTAG = “Finish”;
const string FUELTAG = “Fuel”;
const string FRIENDLYTAG = “Friendly”;
int currentSceneIndex;
int maxLevels;
void Start()
{
currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
maxLevels = SceneManager.sceneCountInBuildSettings;
}
void OnCollisionEnter(Collision other)
{
switch (other.gameObject.tag)
{
case FINISHTAG:
Debug.Log("Collided with: " + FINISHTAG);
currentSceneIndex++;
LevelHandler();
break;
case FUELTAG:
Debug.Log("Collided with: " + FUELTAG);
break;
case FRIENDLYTAG:
Debug.Log("Collided with: " + FRIENDLYTAG);
break;
default:
Debug.Log("You Exploded!!!");
LevelHandler();
break;
}
}
void LevelHandler()
{
Debug.Log(currentSceneIndex);
if (currentSceneIndex == maxLevels)
{
Debug.Log("Congratulations!!! YOU WON!!");
currentSceneIndex = 0;
}
SceneManager.LoadScene(currentSceneIndex);
}
}