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. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How To Spawn Prefab Of Different Versions Of Level Properly

Discussion in 'Scripting' started by Dogg, Oct 21, 2015.

  1. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    Hello Unity community! I've been trying to accomplish something with much trial and error, but I haven't been able to get it to work. You see I'm trying to at the end of a level, destroy the parent game object of the environment, spawn in a new environment, and change the position of the Player and Camera to where they originally started in the level, and repeat that in a endless cycle until the Player dies of course. However I cannot find a way to accomplish this fully.

    I've tried spawning in the prefab and destroying the original level, but the prefab does not have the current camera and Player character saved in the scripts(the public game object slots that use the camera and player are not filled). So I tried just spawning it in with a new camera and player, but the problem with this solution is my score. If I destroy the Player, the score script attached to it would be destroyed as well. So the score that the Player got would be destroyed and not remembered for the new environment.

    What do you guys think I should do? Maybe if I can on prefab load, get my score which was saved and then use it as the current score(but still function like normal) then maybe it would work. But I'm not so sure.

    Here's my score script for anyone who is interested:

    Code (CSharp):
    1. float timer;
    2.  
    3.     public float f_Multiplier = 60; // in the units of seconds lets say.
    4.     public float f_MaxScore = 1000; // maximum score in one try, lets say 1000.
    5.     private float f_Score;
    6.     public Font MyFont;
    7.  
    8.     int minutes;
    9.     int seconds;
    10.  
    11.     public bool TimeGo = true;
    12.  
    13.     private float f_Total;
    14.    
    15.     private float orginalWidth = 900;
    16.     private float orginalHeight = 600;
    17.     private Vector3 scale;
    18.    
    19.  
    20.     void OnDisable()
    21.     {
    22.         f_Score = (f_MaxScore * f_Multiplier) / f_Total;
    23.         //PlayerPrefs.SetInt ("Stage3Scene2Part1", (int)(f_Score));
    24.         PlayerPrefs.SetInt ("Stage3Scene2Part1", (int)(timer));
    25.         PlayerPrefs.SetString("PlayerNameStage3Scene2", "High Score Stage3Scene2Part1");
    26.         PlayerPrefs.Save ();
    27.     }
    28.  
    29.     public void IncreaseScore(int amount)
    30.     {
    31.         timer += amount;
    32.     }
    33.  
    34.     void Update()
    35.     {
    36.         if (TimeGo == true) {
    37.             timer++;
    38.         }
    39.  
    40.         if(minutes > 0)
    41.             f_Total = (minutes * 60) + seconds;
    42.         else
    43.             f_Total = seconds;
    44.     }
    45.  
    46.  
    47.     void OnGUI()
    48.     {
    49.        
    50.         scale.x = Screen.width / orginalWidth;
    51.         scale.y = Screen.height / orginalHeight;
    52.         scale.z = 1; var svMat = GUI.matrix;
    53.         GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, scale); //Gui Labels//
    54.  
    55.  
    56.         int minutes = Mathf.FloorToInt(timer / 60F);
    57.         int seconds = Mathf.FloorToInt(timer - minutes * 60);
    58.         string niceTime = string.Format("{0:00:00}:{1:00}", minutes, seconds);
    59.  
    60.         GUI.color = Color.black;
    61.         GUI.skin.font = MyFont;
    62.         GUI.Label(new Rect(10,10,250 * 5,100 * 4), niceTime);
    63.  
    64.         GUI.matrix = svMat;
    65.  
    66.     }
    67. }
    68.  
    By the way I haven't really worked on the score script much. So there's no set scoring system yet.
     
  2. mafiadude1

    mafiadude1

    Joined:
    Jun 28, 2012
    Posts:
    59
    Why not have your important Game Objects outside of the prefab? This way, you can change the level as you want without affecting the core game.
     
  3. Dogg

    Dogg

    Joined:
    Mar 5, 2014
    Posts:
    203
    I see what you mean, but this could get complicated. A lot of the scripts use triggers which then disable the collider after triggering, to avoid having too much colliders active in-game. I guess I could try it, but I may run into memory issues(which I already have, game sometimes crashes on mobile, and frame rate drops on the first few seconds of the level mobile). Thank you for your help, I'll see if that works later on.
     
  4. mafiadude1

    mafiadude1

    Joined:
    Jun 28, 2012
    Posts:
    59
    Not sure I follow but maybe you can use a global event manager to handle the traffic of triggers.