Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

Triggering even based on score

Discussion in 'Getting Started' started by createappeal, Jul 24, 2019.

  1. createappeal

    createappeal

    Joined:
    Jul 10, 2019
    Posts:
    13
    New to Unity3d and wanted to know how I go about starting an event or cutscene once a player reaches a certain score in a space shooter game. So let's say they get to a score of 50,000 and I want to trigger a cutscene then boss fight. How or what is the scripting I am looking for. Thank you in advance!!!!
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Oversimplified example:
    Code (csharp):
    1. private int score = 0;  //The current score
    2. private int scoreToTriggerBossFight = 50000;  //The score which triggers a boss fight
    3.  
    4. //Call this to add to the score, such as when you defeat an enemy
    5. public void AddToScore(int addToScore)
    6. {
    7.   score += addToScore;
    8. }
    9.  
    10. //Return the current score
    11. //Can also use a getter, or make score public
    12. //But doing it this way better protects than a public variable
    13. //and is usually easier to understand for a novice
    14. public int GetCurrentScore()
    15. {
    16.     return score;
    17. }
    18.  
    19. //Check if the score reaches the level which should trigger the boss fight every frame
    20. //You could do this check instead in AddToScore, but is cleaner here if you have other things
    21. //which change the score other than AddToScore, such as subtracting from score, etc
    22. private void Update()
    23. {
    24.     if (score >= scoreToTriggerBossFight)
    25.     {
    26.         startBossFight();
    27.     }
    28. }
    29.  
    30. //Call this to kick off a boss fight
    31. private void startBossFight()
    32. {
    33.     //Add whatever code here to start your boss fight
    34.     //I'll just assume though that you do so by switching to the BossFight scene
    35.     SceneManagement.SceneManager.LoadScene("BossFight");
    36. }
     
    Last edited: Jul 24, 2019
    createappeal and JoeStrout like this.
  3. createappeal

    createappeal

    Joined:
    Jul 10, 2019
    Posts:
    13
    Hey, cannot thank you enough. I really appreciate it!
     
    Joe-Censored likes this.