Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by RedEyedDeveloper · Jun 08, 2017 at 10:47 AM · c#unity5arraysgameobjectslists

Trying to program two buttons to appear when the player in my game dies

Hello,

I am writing to ask for some assistance please. I am trying to program two buttons a Main menu and Exit button in my game to appear only when my player dies. I have been trying to define the gameobject list with,

     public List<GameObject> menuButtonList = new List<GameObject>();

These are my scipts.

GameManager

 //Awake is always called before any Start functions
 void Awake()
 {
     //Check if instance already exists
     if (instance == null)

         //if not, set instance to this
         instance = this;

     //If instance already exists and it's not this:
     else if (instance != this)

         //Then destroy this. This enforces our singleton pattern, meaning there can only ever be one instance of a GameManager.
         Destroy(gameObject);

     //Sets this to not be destroyed when reloading scene
     DontDestroyOnLoad(gameObject);

     //Assign enemies to a new List of Enemy objects.
     enemies = new List<Enemy>();

     //Get a component reference to the attached BoardManager script
     boardScript = GetComponent<BoardManager>();

     //Call the InitGame function to initialize the first level 
     //InitGame();
 }

 // TODO: Uncommented Code
 /// <summary>
 /// Called when the level / scene has finished loading
 /// </summary>
 /// <param name="scene"></param>
 /// <param name="mode"></param>
 void OnLevelFinishedLoading(Scene scene, LoadSceneMode mode)
 {

     // Don't initialize game on main menu. That would be silly
     if (scene.buildIndex > 0)
     {
         InitGame();
     }

     level++; // Increment after we have loaded level. Might be a better place to put this
 }

 // TODO: Uncomemnted code
 void OnEnable()
 {
     SceneManager.sceneLoaded += OnLevelFinishedLoading;
 }

 void OnDisable()
 {
     SceneManager.sceneLoaded -= OnLevelFinishedLoading;
 }

 //Initializes the game for each level.
 void InitGame()
 {
     //While doingSetup is true the player can't move, prevent player from moving while title card is up.
     doingSetup = true;

     //Get a reference to our image LevelImage by finding it by name.
     levelImage = GameObject.Find("LevelImage");

     //Get a reference to our text LevelText's text component by finding it by name and calling GetComponent.
     levelText = GameObject.Find("LevelText").GetComponent<Text>();

     //Set the text of levelText to the string "Day" and append the current level number.
     levelText.text = "Day " + level;

     //Set levelImage to active blocking player's view of the game board during setup.
     levelImage.SetActive(true);

     //Call the HideLevelImage function with a delay in seconds of levelStartDelay.
     Invoke("HideLevelImage", levelStartDelay);

     //Clear any Enemy objects in our List to prepare for next level.
     enemies.Clear();

     //Call the SetupScene function of the BoardManager script, pass it current level number.
     boardScript.SetupScene(level);

     // TODO: Removed initGame() from here. No need to call it twice
 }


 //Hides black image used between levels
 void HideLevelImage()
 {
     //Disable the levelImage gameObject.
     levelImage.SetActive(false);

     //Set doingSetup to false allowing player to move again.
     doingSetup = false;
 }

 
 //Update is called every frame.
 void Update()
 {
     //Check that playersTurn or enemiesMoving or doingSetup are not currently true.
     if (playersTurn || enemiesMoving || doingSetup)

         //If any of these are true, return and do not start MoveEnemies.
         return;

     //Start moving enemies.
     StartCoroutine(MoveEnemies());
 }

 //Call this to add the passed in Enemy to the List of Enemy objects.
 public void AddEnemyToList(Enemy script)
 {
     //Add Enemy to List enemies.
     enemies.Add(script);
 }




 public void ShowButtons()
 {
     foreach (var Button in buttons)
     {
         var gameOver = GetComponent<Exit2>();
         Button.gameObject.SetActive(true);
     }
 }








 //GameOver is called when the player reaches 0 food points
 public void GameOver()
 {
     //Set levelText to display number of levels passed and game over message
     levelText.text = "After " + level + " days, you starved.";


     //Shows buttons on player's death.
     var gameOver = GetComponent<Exit2>();
     gameOver.ShowButtons("Main menu 2", "Exit2");

     //Enable black background image gameObject.
     levelImage.SetActive(true);

     //Disable this GameManager.
     enabled = false;
 }

 //Coroutine to move enemies in sequence.
 IEnumerator MoveEnemies()
 {
     //While enemiesMoving is true player is unable to move.
     enemiesMoving = true;

     //Wait for turnDelay seconds, defaults to .1 (100 ms).
     yield return new WaitForSeconds(turnDelay);

     //If there are no enemies spawned (IE in first level):
     if (enemies.Count == 0)
     {
         //Wait for turnDelay seconds between moves, replaces delay caused by enemies moving when there are none.
         yield return new WaitForSeconds(turnDelay);
     }

     //Loop through List of Enemy objects.
     for (int i = 0; i < enemies.Count; i++)
     {
         //Call the MoveEnemy function of Enemy at index i in the enemies List.
         enemies[i].MoveEnemy();

         //Wait for Enemy's moveTime before moving next Enemy, 
         yield return new WaitForSeconds(enemies[i].moveTime);
     }
     //Once Enemies are done moving, set playersTurn to true so player can move.
     playersTurn = true;

     //Enemies are done moving, set enemiesMoving to false.
     enemiesMoving = false;
 }



Exit2 Script

     public GameObject menubuttonlist;
     public List<GameObject> menuButtonList = new List<GameObject>();
     unityGameObjects.Add();


   


 void Awake()
 {
     // Get the buttons
     menuButtonList ();
     //buttons = GetComponentsInChildren<Button>();

     // Disable them
     HideButtons();
 }

 public void HideButtons()
 {
     foreach (var Button in buttons)
     {
         Button.gameObject.SetActive(false);
     }
 }

   
 public void LoadByIndex(int sceneIndex)
 {
     SceneManager.LoadScene(sceneIndex);
 }

 public void Quit()
 {

if UNITY_EDITOR

    UnityEditor.EditorApplication.isPlaying = false;

else

    Application.Quit ();

endif

}

I just want to make it so in my Gamemanager script under GameOver function is called the two buttons appear with what s already defined in the game PLEASE CHECK the lines in Gamemanager 162-194 and all of Exit2.

Any help will be greatly appreciated.

Thank you.

Sincerely,

N.R.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

369 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Only spawning power ups that the player wants in that game 1 Answer

C# List index confusion using Mathf.Clamp 1 Answer

Why is my List adding to same array object again and again in update? 1 Answer

Endless Runner with new Biome each time 0 Answers

Trying to get gameobject from one list to follow gameobjects in another list 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges