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

Scenes loading fine in Unity Play Mode but not in Build.

Discussion in 'Editor & General Support' started by JustJat, Jan 16, 2020.

  1. JustJat

    JustJat

    Joined:
    Jan 13, 2018
    Posts:
    2
    I am making a simple game and it has been working fine until recently. I added a Main Screen and a scene loading script but now whenever I click on the button it doesn't do anything. In the editor it doesn't show any errors and it works perfectly. I am building to Andriod but when I use Unity Remote to test it works perfectly as well.

    Here is my code for the scene loading:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.UI;
    6. public class LoadSceneOnClick : MonoBehaviour
    7. {
    8.     public Object scene; //The game scene that will be loaded.
    9.  
    10.     private void Start()
    11.     {
    12.         GetComponent<Button>().onClick.AddListener(startScene);
    13.     }
    14.     private void startScene()
    15.     {
    16.         SceneManager.LoadSceneAsync(scene.name);
    17.     }
    18. }
    19.  
    Even when I use that for scene reloading it doesn't work.

    I heard something about it not working if there are things that try to access elements from other scenes but I don't know if it is true. I have a static class that I use so the player can configure some properties of the game (Like speed).

    Here is the static class I use to store these changes. (I don't think it is the problem, but just in case):

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public static class MainScreenSettings : object
    7. {
    8.     static public int rowsOfPlayers = 1; //Rows of players
    9.     static public float speed = 10; //speed of the player
    10.     static public float minDist = 10; //Min dist between obstacles
    11.     static public float maxDist = 30; //Max dist between obstacles
    12.     static public float initialDist = 40; //Starting distance between player and first obstacle
    13.  
    14.     public static ArrayList getSettings()
    15.     {
    16.  
    17.         return new ArrayList(){rowsOfPlayers, speed, minDist, maxDist, initialDist};
    18.     }
    19.  
    20. }
    21.  
    Here is the script that gets the input and changes the static script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. public class SetMainSettings : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.         MainScreenSettings.speed = transform.Find("Speed").gameObject.GetComponent<Slider>().value;
    11.         MainScreenSettings.rowsOfPlayers = (int)transform.Find("Rows").gameObject.GetComponent<Slider>().value;
    12.         MainScreenSettings.minDist = transform.Find("MinDistance").gameObject.GetComponent<Slider>().value;
    13.         MainScreenSettings.maxDist = transform.Find("MaxDistance").gameObject.GetComponent<Slider>().value;
    14.         MainScreenSettings.initialDist = transform.Find("StartDistance").gameObject.GetComponent<Slider>().value;
    15.         transform.Find("Speed").gameObject.GetComponent<Slider>().onValueChanged.AddListener(speed);
    16.         transform.Find("Rows").gameObject.GetComponent<Slider>().onValueChanged.AddListener(rows);
    17.         transform.Find("MinDistance").gameObject.GetComponent<Slider>().onValueChanged.AddListener(minDist);
    18.         transform.Find("MaxDistance").gameObject.GetComponent<Slider>().onValueChanged.AddListener(maxDist);
    19.         transform.Find("StartDistance").gameObject.GetComponent<Slider>().onValueChanged.AddListener(startDist);
    20.     }
    21.     private void speed(float val)
    22.     {
    23.         MainScreenSettings.speed = val;
    24.     }
    25.     private void rows(float val)
    26.     {
    27.         MainScreenSettings.rowsOfPlayers = (int)val;
    28.     }
    29.     private void minDist(float val)
    30.     {
    31.         if (val > MainScreenSettings.maxDist)
    32.         {
    33.             transform.Find("MinDistance").gameObject.GetComponent<Slider>().value = MainScreenSettings.maxDist - 1;
    34.         }
    35.         else
    36.         {
    37.             MainScreenSettings.minDist = val;
    38.         }
    39.     }
    40.     private void maxDist(float val)
    41.     {
    42.         if (val < MainScreenSettings.minDist)
    43.         {
    44.             transform.Find("MaxDistance").gameObject.GetComponent<Slider>().value = MainScreenSettings.minDist + 1;
    45.         }
    46.         else
    47.         {
    48.             MainScreenSettings.maxDist = val;
    49.         }
    50.     }
    51.     private void startDist(float val)
    52.     {
    53.         MainScreenSettings.initialDist = val;
    54.     }
    55. }
    56.  
    And here is the code that gets the stats from the MainSceneSettings class and uses them to change the values of other scripts. (This is in the second scene so I may be the problem?)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MainSettings : MonoBehaviour
    6. {
    7.     //Path Rows!
    8.     [System.Serializable]
    9.     public class PathRows
    10.     {
    11.         public int rowsOfPlayers = 1; //Rows of players
    12.         public float distBetween = 5; //Dist between rows horizontally
    13.         public float curDisX = 0; //will hold the current X pos of the row. Used to increament the row dist between.
    14.     }
    15.     public PathRows pathRows = new PathRows();
    16.     //Player Settings!
    17.     [System.Serializable]
    18.     public class PlayerSettings
    19.     {
    20.         public float speed = 10; //speed of the player
    21.         public float smashTimer = 0.5f; //Time before smash runs out
    22.         public Color defColor = Color.red; //Default color of player
    23.         public Color smashColor = Color.black; //Color for smashing.
    24.     }
    25.     public PlayerSettings playerSettings = new PlayerSettings();
    26.     //Camera Settings
    27.     [System.Serializable]
    28.     public class CameraSettings
    29.     {
    30.         public float yDist = 3.3f; //This will be the distance from the player to the camera on the y axis
    31.         public float cameraZ = -2.65f; //This will be the distance from the player to the camera on the z axis
    32.     }
    33.     public CameraSettings cameraSettings = new CameraSettings();
    34.  
    35.     //Obstacle Settings!
    36.     [System.Serializable]
    37.     public class ObstacleSettings
    38.     {
    39.         public int obVal = 100; //The obstacle value
    40.         public float minDist = 10; //Min dist between obstacles
    41.         public float maxDist = 30; //Max dist between obstacles
    42.         public int loadAmt = 40; //Max amt of obstacles to be loaded at one time
    43.         public int amtToLoadAt = 10; //amt of obstacles before more are loaded.
    44.         public float initialDist = 20; //Starting distance between player and first obstacle
    45.     }
    46.     public ObstacleSettings obstacleSettings = new ObstacleSettings();
    47.    
    48.     public void Awake()
    49.     {
    50.         //GETS MAIN SCREEN SETTINGS
    51.         pathRows.rowsOfPlayers = MainScreenSettings.rowsOfPlayers;
    52.         playerSettings.speed = MainScreenSettings.speed;
    53.         obstacleSettings.minDist = MainScreenSettings.minDist;
    54.         obstacleSettings.maxDist = MainScreenSettings.maxDist;
    55.         obstacleSettings.initialDist = MainScreenSettings.initialDist;
    56.         //CONTROLS PATH CREATION
    57.         GameObject path = GameObject.FindGameObjectWithTag("Paths");
    58.         for(int i = 1; i<=pathRows.rowsOfPlayers-1; i++) //adds new row for each player (start -1 cause one is already added)
    59.         {
    60.             pathRows.curDisX += pathRows.distBetween;//Get the next x pos
    61.             //Create new path
    62.             GameObject newPath = GameObject.Instantiate(path, new Vector3(pathRows.curDisX, path.transform.position.y, path.transform.position.z),path.transform.rotation);
    63.  
    64.         }
    65.         //CONTROLS speed, SMASHTIMER, DEFAULT COLOR, and SMASH COLOR
    66.         foreach(GameObject plr in GameObject.FindGameObjectsWithTag("Player"))
    67.         {
    68.             PlayerMain pM = plr.GetComponent<PlayerMain>(); //Gets the script
    69.             pM.speed = playerSettings.speed; //Sets speed
    70.             pM.smashTimer = playerSettings.smashTimer; //Sets smash timer
    71.             pM.defColor = playerSettings.defColor; //Sets default color
    72.             pM.smashColor = playerSettings.smashColor; //Sets smashColor
    73.         }
    74.         //CONTROLS CAMERA DIST FROM PLAYER
    75.         GameObject camera = Camera.main.gameObject;
    76.         CameraController cC = camera.GetComponent<CameraController>();
    77.         cC.yDist = cameraSettings.yDist; //This will be the distance from the player to the camera on the y axis
    78.         cC.cameraZ = cameraSettings.cameraZ; //This will be the distance from the player to the camera on the z axis
    79.         //CONTROLS OBSTACLES (VALUE, DIST, INITIAL DIST, MAX AMT TO LOAD, and AMT TO LOAD AT)
    80.         foreach (GameObject ob in GameObject.FindGameObjectsWithTag("Obstacles"))
    81.         {
    82.             ObstacleHandler oH = ob.GetComponent<ObstacleHandler>(); //Gets the script
    83.             oH.obstacleValue = obstacleSettings.obVal; //Sets object Value
    84.             oH.obDistMin = obstacleSettings.minDist; //Sets minimum dist between obstacles
    85.             oH.obDistMax = obstacleSettings.maxDist; //Sets max dist between obstacles
    86.             oH.maxObstaclesLoaded = obstacleSettings.loadAmt; //Sets the max amt of obstacles that can be loaded at once
    87.             oH.loadObstaclesAmt = obstacleSettings.amtToLoadAt; //Sets the amt of obstacles to start loading more at.
    88.             oH.curObDist = obstacleSettings.initialDist; //Sets the intial distance between the player and the obstacles.
    89.         }
    90.     }
    91. }
    92.  


    It all works perfectly in the editor play mode but doesn't work in build mode. I think the button registers the click because it changes color when hovered over, tapped, and selected.

    Does anyone have any idea how to fix this?