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

Can't get Play Button to work

Discussion in 'Scripting' started by dyach3579, Apr 13, 2020.

  1. dyach3579

    dyach3579

    Joined:
    Jan 3, 2018
    Posts:
    22
    Hi, I want to start by saying I'm still new to unity and not very advanced in my programming yet. And right now Im having issues with my Start button not working.

    Basically after I lose, my Game Over Panel pops up and you can choose to "Play Again" or go to "Main Menu." When I choose the Main Menu, on the Menu itself there's a "Play Game" button that will take you back to the Game (and I'll have some other buttons on there as well soon). So when I hit the "Play Game" button it takes me back to the Game, then you're suppose to hit the "Play" button to start actually playing, but the "Play" button won't start the game. It just sits there and turns a different color.

    Anybody know where I messed up in the code to get the Play button to stop working? Its probably something simple in my GameManager code. Here is the code and I appreciate any help. And for some reason my code is not indented properly when put it on this forum.

    Code (CSharp):
    1. public class GameManager : MonoBehaviour
{

  
    2.  
    3. public Sprite[ ] ColoredPlayer;  
    4. public Player player;
  
    5. public Spawner spawner;

  
    6. public static GameManager instance;

  
    7. public bool bGameStarted = false;

  
    8.  
    9. public GameObject MainMenu;
  
    10. public GameObject GameHub;
  
    11. public GameObject gameOverPanel;
    12.  
    13. public int score = 0;
  
    14. public int bestScore;
  
    15. public static int scoreIndex = 0;
  
    16. public float timeToPauseOnGameOver = 1.2f;
  
    17. public Text txtScore;
  
    18. public Text BestScoreTxt;

  
    19. public static int[] scores = { 0, 0, 0, 0 };
  
    20. GameOverScreen;
  
    21. public GameObject[] myPlayerPrefabs;
  
    22. public GameObject[] myPlayerObjects;


    23.  
    24. [SerializeField] AudioClip bubblesSound;
  
    25. [SerializeField] [Range(0, 1)] float bubblesSoundVolume = 0.75f;

  
    26.  
    27. private void Awake()
  
    28. {
    
    29.     if (instance == null)
      
    30.     {
    
    31.          instance = this;
          
    32.          DontDestroyOnLoad(gameObject);
      
    33.      }
      
    34.       else
    
    35.       {
          
    36.          Destroy(gameObject);
      
    37.       }
      
    38.  
    39.       bestScore = PlayerPrefs.GetInt("BestScore", 0);
      
    40.       BestScoreTxt.text = "Best: " + bestScore.ToString();
      
    41.       score = 0;
      
    42.       UpdateScoreText();
  
    43.  }

  
    44.  
    45.       public void OnClickPlayButton()
    46.        {
      
    47.          score = 0;
      
    48.          UpdateScoreText();
      
    49.          bGameStarted = true;


    50.      
    51.          for(int i = 0; i < myPlayerObjects.Length; i++)
    52.         {
    53.             myPlayerObjects[i].SetActive(true);
    54.             myPlayerObjects[i].transform.position = new Vector3(0, myPlayerObjects[i].transform.position.y, myPlayerObjects[i].transform.position.z);
    55.             myPlayerObjects[i].GetComponent<Player>().startDragMove = false;
    56.         }
      
    57.        spawner.StartObstaclesMove();
        
      
    58.        GameHub.SetActive(false);
    
    59.     }
    60.  
    61.     public void BackToMenu()
    62.     {
    63.         SceneManager.LoadScene("MainMenu");
    64.     }
    65.  
    66.     public void GameOver()
  
    67.     {
      
    68.         bGameStarted = false;
      
    69.         spawner.StopSpawning(); 
        
      
    70.      
    71.        Invoke("BringUpGameOverScreen",timeToPauseOnGameOver);


    72.    
    73.        gameOverPanel.SetActive(true);
    74.      }

  
    75.  
    76.     private void BringUpGameOverScreen()
    77.     {
      
    78.        MainMenu.SetActive(true);
      
    79.        GameHub.SetActive(true);
  
    80.     }

  
    81.  
    82.      public void updatePlayer(Player player)
    83.      {
    84.         //Random.InitState((int)System.DateTime.Now.Ticks);
    85.  
    86.         int playerIndex = Random.Range(0, myPlayerObjects.Length - 1);
    87.      
    88.         while (myPlayerObjects[playerIndex].name == player.gameObject.name)
    89.         {
    90.             playerIndex = Random.Range(0, myPlayerObjects.Length - 1);
    91.         }
    92.  
    93.         Player myPlayerData = myPlayerObjects[playerIndex].GetComponent<Player>();
    94.         Sprite myPlayerSprite =   myPlayerObjects[playerIndex].GetComponent<SpriteRenderer>().sprite;
    95.         float tempY = myPlayerObjects[playerIndex].transform.position.y;
    96.  
    97.         myPlayerData.LeftKey = player.LeftKey;
    98.         myPlayerData.RightKey = player.RightKey;
    99.      
    100.         myPlayerObjects[playerIndex].transform.position = new Vector3(myPlayerObjects[playerIndex].transform.position.x, player.transform.position.y, 0);
    101.         myPlayerData.yBounds = myPlayerObjects[playerIndex].transform.position.y;
    102.  
    103.         player.LeftKey = myPlayerData.LeftKey;
    104.         player.RightKey = myPlayerData.RightKey;
    105.      
    106.         player.transform.position = new Vector3(player.transform.position.x, tempY, 0);
    107.         player.yBounds = player.transform.position.y;
    108.  
    109.         AudioSource.PlayClipAtPoint(bubblesSound, Camera.main.transform.position, bubblesSoundVolume);//for position changer sound
    110.     }

  
    111.  
    112.      public void IncreaseScore(int amount = 1)
  
    113.      {
      
    114.          score += amount;

  
    115.  
    116.         UpdateScoreText();
      
    117.  
    118.         if (score > bestScore)
      
    119.        {
          
    120.          bestScore = score;
          
    121.          BestScoreTxt.text = "Best: " + bestScore.ToString();
            
    122.          PlayerPrefs.SetInt("BestScore", score);
                
    123.          PlayerPrefs.Save();
      
    124.         }
  
    125.     }

  
    126.  
    127. private void UpdateScoreText()
  
    128. {
    129.         txtScore.text = "Score: " + score.ToString();
  
    130. }
    131. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    First thing is always, "are there any errors in the console output?"

    The next thing is to put in strategic
    Debug.Log( GetType() + ": I got here to point 123!");
    statements in your code to make sure the code you want is running. Some places to put it might be in the Start() function of your scripts, and also in the handling function for that button. The idea is you have to find: is the button even calling the function? Is the function not doing what it needs? Is something the function calls not doing what it needs? etc.
     
  3. dyach3579

    dyach3579

    Joined:
    Jan 3, 2018
    Posts:
    22
    Hi Kurt. There’s no errors that come up when I click the button. When I use that Debug code you gave me it comes up In the console when I run the game. But I’m still not sure what going on
     
  4. dyach3579

    dyach3579

    Joined:
    Jan 3, 2018
    Posts:
    22
    The Play button works when I first run the game. It’s when I lose, then I choose to go back to the main menu, but then decide to play again so I click “play game” to go back to the game then click “play” but nothing happens