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. Dismiss Notice

Player changing

Discussion in 'Scripting' started by Deleted User, Feb 11, 2021.

  1. Deleted User

    Deleted User

    Guest

    there are two scenes in the unity game.

    one scene is shop where you can choose player 1 - player 2 - player 3.

    another scene is gameplay scene.

    what i need to understand is how when user selects each of these players , then in gameplay relevant prefab could be instantiated? how is this possible?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    Keep a game manager (usually a persistent singleton) with every piece of data that you do NOT want to go away between scene changes.

    Look up tutorials on game managers, or you're welcome to take and modify these scripts. Be sure to understand their lifecycle as your scene changes happen.

    Some super-simple Singleton examples to take and modify:

    Simple Unity3D Singleton (no predefined data):

    https://pastebin.com/SuvBWCpJ

    Unity3D Singleton with Prefab used for predefined data:

    https://pastebin.com/cv1vtS6G

    These are pure-code solutions, do not put anything into any scene, just access it via .Instance!

    If it is a GameManager, when the game is over, make a function in that singleton that Destroys itself so the next time you access it you get a fresh one, something like:

    Code (csharp):
    1. public void DestroyThyself()
    2. {
    3.    Destroy(gameObject);
    4.    Instance = null;    // because destroy doesn't happen until end of frame
    5. }
     
    Joe-Censored likes this.
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Simple example using a static variable. Just set ChosenPlayer somewhere in the first scene. No need to include the script anywhere in the first scene. Attach the script to some GameObject in the second scene, and add the Player prefabs to the PlayerPrefabs array via the inspector in the same order as in the SelectedPlayer enum. Then call InstantiatePlayer() to spawn the correct Player prefab.

    Code (csharp):
    1. public enum SelectedPlayer {Player1, Player2, Player3};
    2. public static SelectedPlayer ChosenPlayer;  //Set this in the first scene when selecting the player, use Class.ChosenPlayer, where Class is the name of the class where this code lives
    3. //There is no need to actually attach this script to a GameObject in the first scene, only the second scene
    4.  
    5. public GameObject[] PlayerPrefabs;  //With this script attached to a GameObject in the second scene, fill in this array in the inspector
    6. //Place the prefabs in this array in the same order as in the SelectedPlayer enum
    7.  
    8. //Call this method in the second scene to instantiate and return the chosen player prefab
    9. public GameObject InstantiatePlayer()
    10. {
    11.     return Instantiate(PlayerPrefabs[(int)ChosenPlayer]);
    12. }
    There's lots of ways of achieving the same goal though. Basically all you are trying to do is transfer information from one scene to the next. Some alternative approaches could be using a DontDestroyOnLoad GameObject, or using additive scenes so you don't actually exit the first scene when you load the second.
     
    Deleted User likes this.
  4. Deleted User

    Deleted User

    Guest

    i used your script in my shop in the following script :

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class BuyScript2 : MonoBehaviour
    4. {
    5.     private int myChar;
    6.  
    7.     public int price;
    8.  
    9.     // Set this in the first scene when selecting the player, use Class.ChosenPlayer,
    10.     // where Class is the name of the class where this code lives
    11.     public enum SelectedPlayer { Player1, Player2, Player3 };
    12.  
    13.  
    14.  
    15.     //There is no need to actually attach this script to a GameObject in the first scene, only the second scene
    16.     public static SelectedPlayer ChosenPlayer;
    17.  
    18.     //With this script attached to a GameObject in the second scene, fill in this array in the inspector
    19.     //Place the prefabs in this array in the same order as in the SelectedPlayer enum
    20.     public GameObject[] PlayerPrefabs;
    21.  
    22.  
    23.     //Call this method in the second scene to instantiate and return the chosen player prefab
    24.     public GameObject InstantiatePlayer()
    25.     {
    26.         return Instantiate(PlayerPrefabs[(int)ChosenPlayer]);
    27.     }
    28.  
    29.  
    30.     void Awake()
    31.     {
    32.         myChar = GetComponent<BuyScript>().myChar;
    33.     }
    34.  
    35.     void Update()
    36.     {
    37.         if (myChar == 1)
    38.         {
    39.             GlobalValues.character = 1;
    40.             PersistentData.Save();
    41.         }
    42.  
    43.         if (myChar == 2)
    44.         {
    45.             if (GlobalValues.character2Unlocked)
    46.             {
    47.                 GlobalValues.character = 2;
    48.                 // player 2 is unlocked, bring them in the gameplay scene
    49.  
    50.                 PersistentData.Save();
    51.             }
    52.             else
    53.             {
    54.                 if (GlobalValues.money >= price)
    55.                 {
    56.                     GlobalValues.money = GlobalValues.money - price;
    57.                     GlobalValues.character2Unlocked = true;
    58.                     PersistentData.Save();
    59.                 }
    60.             }
    61.         }
    62.  
    63.         if (myChar == 3)
    64.         {
    65.             if (GlobalValues.character3Unlocked)
    66.             {
    67.                 GlobalValues.character = 3;
    68.                 PersistentData.Save();
    69.  
    70.             }
    71.             else
    72.             {
    73.                 if (GlobalValues.money >= price)
    74.                 {
    75.                     GlobalValues.money = GlobalValues.money - price;
    76.                     GlobalValues.character3Unlocked = true;
    77.                     PersistentData.Save();
    78.                 }
    79.             }
    80.         }
    81.  
    82.         GetComponent<BuyScript2>().enabled = false;
    83.     }
    84. }
    85.  
    this script is attached to each ui button that is used to buy new player. is it right?

    what do i do now in gameplay scene if untill here i did correctly. thanks.
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    The code I posted goes in a script in the 2nd scene, not the 1st. Isn't your buy script in the 1st scene?

    In your first scene you just need some buttons, which call a function, which just sets the static variable "ChosenPlayer". Since it is static, you don't even need an instance of the script in the scene in order to set that variable.

    Code (csharp):
    1. //Button somewhere in first scene
    2. public void BuyPlayer1ButtonPressed()
    3. {
    4.     SecondSceneWhateverScript.ChosenPlayer = SecondSceneWhateverScript.SelectedPlayer.Player1;  //Replace SecondSceneWhateverScript with actual name of the script
    5. }
    All you do in the 2nd scene is then call InstantiatePlayer(). Done
     
    Deleted User likes this.
  6. Deleted User

    Deleted User

    Guest

    in the first scene , there are 3 ui button to select each player.

    to which of those three button , then i need to assign your first scene method?
     
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I thought the name of the method made it self explanatory. BuyPlayer1ButtonPressed() would be the one you'd call when the player 1 button is pressed. Create another method for players 2 and 3 buttons.
     
    Deleted User likes this.
  8. Deleted User

    Deleted User

    Guest

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class BuyScript2 : MonoBehaviour
    4. {
    5.     private int myChar;
    6.  
    7.     public int price;
    8.  
    9.     public GameObject[] players;
    10.  
    11.  
    12.  
    13.     void Awake()
    14.     {
    15.         myChar = GetComponent<BuyScript>().myChar;
    16.     }
    17.  
    18.     void Update()
    19.     {
    20.         if (myChar == 1)
    21.         {
    22.             GlobalValues.character = 1;
    23.             PersistentData.Save();
    24.         }
    25.  
    26.         if (myChar == 2)
    27.         {
    28.             if (GlobalValues.character2Unlocked)
    29.             {
    30.                 GlobalValues.character = 2;
    31.                 PlayerPrefs.SetInt("selectedCharacter", GlobalValues.character);
    32.                 PersistentData.Save();
    33.             }
    34.             else
    35.             {
    36.                 if (GlobalValues.money >= price)
    37.                 {
    38.                     GlobalValues.money = GlobalValues.money - price;
    39.                     GlobalValues.character2Unlocked = true;
    40.                     PersistentData.Save();
    41.                 }
    42.             }
    43.         }
    44.  
    45.         if (myChar == 3)
    46.         {
    47.             if (GlobalValues.character3Unlocked)
    48.             {
    49.                 GlobalValues.character = 3;
    50.                 PersistentData.Save();
    51.  
    52.             }
    53.             else
    54.             {
    55.                 if (GlobalValues.money >= price)
    56.                 {
    57.                     GlobalValues.money = GlobalValues.money - price;
    58.                     GlobalValues.character3Unlocked = true;
    59.                     PersistentData.Save();
    60.                 }
    61.             }
    62.         }
    63.  
    64.         GetComponent<BuyScript2>().enabled = false;
    65.     }
    66. }
    67.  
    i have those three methods already, may this shows you better what i mean,

    now i wonder where to put what if you explain again please.
     
  9. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    What 3 methods? There's only 2 methods here, and they are Awake and Update.

    But seriously, I'm just suggesting using a static variable to pass the data. I've provided a simple example that you can just copy/paste. If you don't understand how static variables work, then go follow some beginner intro to C# programming tutorials which cover them. They actually have nothing to do with Unity, but you do need to understand how they work, and I can't teach you the entire C# language via forum posts.
     
    Kurt-Dekker likes this.