Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

loading 2 scenes at the same time

Discussion in 'Editor & General Support' started by Digital_Owl, Dec 3, 2021.

  1. Digital_Owl

    Digital_Owl

    Joined:
    Feb 7, 2021
    Posts:
    46
    whenever my player dies game over UI scene is loaded twice, this only happens whenever i add don't destroy on load code to the script that counts life, i can't interact with the scene. any idea why?





    Code (CSharp):
    1. using System.Collections;
    2.  
    3. using System.Collections.Generic;
    4.  
    5. using UnityEngine;
    6.  
    7. using UnityEngine.SceneManagement;
    8.  
    9.  
    10.  
    11. public class GameControlScript : MonoBehaviour
    12.  
    13. {
    14.  
    15. public GameObject heart1, heart2, heart3, heart4;
    16.  
    17. public static int health;
    18.  
    19. void Start()
    20.  
    21. {
    22.  
    23. health = 4;
    24.  
    25. heart1.gameObject.SetActive (true);
    26.  
    27. heart2.gameObject.SetActive(true);
    28.  
    29. heart3.gameObject.SetActive(true);
    30.  
    31. heart4.gameObject.SetActive(true);
    32.  
    33. }
    34.  
    35.  
    36.  
    37.  
    38.  
    39. void Update()
    40.  
    41. {
    42.  
    43. if (health > 4)
    44.  
    45. health = 4;
    46.  
    47.  
    48.  
    49. switch (health) {
    50.  
    51. case 4:
    52.  
    53. heart1.gameObject.SetActive(true);
    54.  
    55. heart2.gameObject.SetActive(true);
    56.  
    57. heart3.gameObject.SetActive(true);
    58.  
    59. heart4.gameObject.SetActive(true);
    60.  
    61. break;
    62.  
    63.  
    64.  
    65. case 3:
    66.  
    67. heart1.gameObject.SetActive(true);
    68.  
    69. heart2.gameObject.SetActive(true);
    70.  
    71. heart3.gameObject.SetActive(true);
    72.  
    73. heart4.gameObject.SetActive(false);
    74.  
    75. break;
    76.  
    77.  
    78.  
    79. case 2:
    80.  
    81. heart1.gameObject.SetActive(true);
    82.  
    83. heart2.gameObject.SetActive(true);
    84.  
    85. heart3.gameObject.SetActive(false);
    86.  
    87. heart4.gameObject.SetActive(false);
    88.  
    89. break;
    90.  
    91.  
    92.  
    93. case 1:
    94.  
    95. heart1.gameObject.SetActive(true);
    96.  
    97. heart2.gameObject.SetActive(false);
    98.  
    99. heart3.gameObject.SetActive(false);
    100.  
    101. heart4.gameObject.SetActive(false);
    102.  
    103. break;
    104.  
    105.  
    106.  
    107. case 0:
    108.  
    109. heart1.gameObject.SetActive(false);
    110.  
    111. heart2.gameObject.SetActive(false);
    112.  
    113. heart3.gameObject.SetActive(false);
    114.  
    115. heart4.gameObject.SetActive(false);
    116.  
    117. SceneManager.LoadScene("LoseScene");
    118.  
    119. break;
    120.  
    121.  
    122.  
    123.  
    124.  
    125. }
    126.  
    127. }
    128.  
    129.  
    130.  
    131.  
    132.  
    133. }
    134.  
    135.  
    136.  
    137.  
    138.  
    139.  
     

    Attached Files:

  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    You're loading the scene in Update, so LoadScene is getting called multiple times.
     
  3. Digital_Owl

    Digital_Owl

    Joined:
    Feb 7, 2021
    Posts:
    46
    where should i move it?