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

character selection

Discussion in '2D' started by hadou357, Jan 18, 2019.

  1. hadou357

    hadou357

    Joined:
    Aug 28, 2018
    Posts:
    22
    I recently just created a characters store in my game where I can unlock characters and buy characters. now I'm trying to figure out how to take my selected character and start the next scene. So I can play with those characters in the game. I was hoping that someone had some Simple Solutions to these issues. any and all dialogue is welcome thank you.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DragonShop : MonoBehaviour {
    6.  
    7.     public static DragonShop dragonShop;
    8.  
    9.     public List<Dragon> dragonList = new List<Dragon>();
    10.  
    11.     private List<GameObject> itemHolderList = new List<GameObject>();
    12.  
    13.     public List<GameObject> buyButtonList = new List<GameObject>();
    14.  
    15.     public GameObject itemHolderPrefab;
    16.     public Transform grid;
    17.  
    18.     public GameObject warningImage;
    19.     private bool warningActive = false;
    20.    
    21.     // Use this for initialization
    22.     void Start () {
    23.         warningImage.SetActive(false);
    24.         dragonShop = this;
    25.         FillList();
    26.  
    27.     }
    28.    
    29.     void FillList()
    30.     {
    31.         for (int i = 0; i < dragonList.Count; i++)
    32.         {
    33.             GameObject holder = Instantiate(itemHolderPrefab,grid,false);
    34.             ItemHolder holderScript = holder.GetComponent<ItemHolder>();
    35.  
    36.             holderScript.itemName.text = dragonList[i].dragonName;
    37.             holderScript.itemPrice.text = "$ " + dragonList[i].dragonPrice.ToString("N2");
    38.             holderScript.itemID = dragonList[i].dragonID;
    39.  
    40.             //BUY BUTTON
    41.             holderScript.buyButton.GetComponent<BuyButton>().dragonID = dragonList[i].dragonID;
    42.  
    43.             //Handle Lists
    44.             itemHolderList.Add(holder);
    45.             buyButtonList.Add(holderScript.buyButton);
    46.  
    47.             if (dragonList[i].bought)
    48.             {
    49.                 holderScript.itemImage.sprite = Resources.Load<Sprite>("Sprites/" + dragonList[i].boughtSpriteName);
    50.             }
    51.             else
    52.             {
    53.                 holderScript.itemImage.sprite = Resources.Load<Sprite>("Sprites/" + dragonList[i].unboughtSpriteName);
    54.             }
    55.  
    56.  
    57.         }
    58.     }
    59.  
    60.     public void UpdateSprite(int dragonID)
    61.     {
    62.         for (int i = 0; i < itemHolderList.Count; i++)
    63.         {
    64.             ItemHolder holderScript = itemHolderList[i].GetComponent<ItemHolder>();
    65.             if(holderScript.itemID == dragonID)
    66.             {
    67.                for(int j = 0; j< dragonList.Count; j++)
    68.                 {
    69.                   if (dragonList[j].dragonID == dragonID)
    70.                     {
    71.                         if (dragonList[j].bought)
    72.                         {
    73.                             holderScript.itemImage.sprite = Resources.Load<Sprite>("Sprites/" + dragonList[i].boughtSpriteName);
    74.                             holderScript.itemPrice.text = "Sold Out!";
    75.                         }
    76.                         else
    77.                         {
    78.                             holderScript.itemImage.sprite = Resources.Load<Sprite>("Sprites/" + dragonList[i].unboughtSpriteName);
    79.                         }
    80.                     }
    81.                 }
    82.             }
    83.         }
    84.     }
    85.  
    86.     public void UpdateBuyButtons()
    87.     {
    88.         int currentDragonID = GameManager.gameManager.currentDragonID;
    89.  
    90.         for (int i = 0; i < buyButtonList.Count; i++)
    91.         {
    92.             BuyButton buyButtonScript = buyButtonList[i].GetComponent<BuyButton>();
    93.             for (int j = 0; j < dragonList.Count; j++)
    94.             {
    95.                 if (dragonList[j].dragonID == buyButtonScript.dragonID && dragonList[j].bought && dragonList[j].dragonID != currentDragonID)
    96.                 {
    97.                     buyButtonScript.buttonText.text = "Use";
    98.                 }
    99.                 else if (dragonList[j].dragonID == buyButtonScript.dragonID && dragonList[j].bought && dragonList[j].dragonID == currentDragonID)
    100.                 {
    101.                     buyButtonScript.buttonText.text = "Using";
    102.                 }
    103.             }
    104.         }
    105.     }
    106.  
    107.     public IEnumerator showWaring()
    108.     {
    109.         if (warningActive)
    110.         {
    111.             yield break;
    112.         }
    113.  
    114.         warningActive = true;
    115.  
    116.         warningImage.SetActive(true);
    117.         yield return new WaitForSeconds(1f);
    118.         warningImage.SetActive(false);
    119.         warningActive = false;
    120.     }
    121.        
    122.    
    123. }
    124.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class BuyButton : MonoBehaviour {
    7.  
    8.     public int dragonID;
    9.     public Text buttonText;
    10.  
    11.     public void BuyDragon() // on click
    12.     {
    13.         if(dragonID == 0)
    14.         {
    15.             Debug.Log("no dragon ID set!!!");
    16.             return;
    17.         }
    18.  
    19.         for (int i = 0; i < DragonShop.dragonShop.dragonList.Count; i++)
    20.         {
    21.             if (DragonShop.dragonShop.dragonList[i].dragonID == dragonID && !DragonShop.dragonShop.dragonList[i].bought && GameManager.gameManager.RequestMoney(DragonShop.dragonShop.dragonList[i].dragonPrice))
    22.             {
    23.                 //buy the dragon
    24.                 DragonShop.dragonShop.dragonList[i].bought = true;
    25.                 GameManager.gameManager.ReduceMoney(DragonShop.dragonShop.dragonList[i].dragonPrice);
    26.                
    27.                 //SET DRAGON ID IN YOUR SYSTEM
    28.                 GameManager.gameManager.currentDragonID = dragonID;
    29.  
    30.                 UpdateBuyButton();
    31.             }
    32.             else if (DragonShop.dragonShop.dragonList[i].dragonID == dragonID && !DragonShop.dragonShop.dragonList[i].bought && !GameManager.gameManager.RequestMoney(DragonShop.dragonShop.dragonList[i].dragonPrice))
    33.             {
    34.                 Debug.Log("NOT ENOUGH MONEY");
    35.                 StartCoroutine(DragonShop.dragonShop.showWaring());
    36.             }
    37.             else if (DragonShop.dragonShop.dragonList[i].dragonID == dragonID && DragonShop.dragonShop.dragonList[i].bought)
    38.             {
    39.                 Debug.Log("Has BEEN BOUGHT ALREADY");
    40.                
    41.                 //SET DRAGON ID IN YOUR SYSTEM
    42.                 GameManager.gameManager.currentDragonID = dragonID;
    43.  
    44.                 UpdateBuyButton();
    45.             }
    46.         }
    47.  
    48.         //DragonShop.dragonShop.UpdateSprite(dragonID);
    49.     }
    50.  
    51.     void UpdateBuyButton()
    52.     {
    53.         /*
    54.         buttonText.text = "Using";
    55.  
    56.         for(int i = 0; i < DragonShop.dragonShop.buyButtonList.Count;i++)
    57.         {
    58.             BuyButton buyButtonScript = DragonShop.dragonShop.buyButtonList[i].GetComponent <BuyButton>();
    59.             for(int j = 0;j< DragonShop.dragonShop.dragonList.Count; j++)
    60.             {
    61.                 //has bought, weapon match
    62.                 if(DragonShop.dragonShop.dragonList[j].dragonID == buyButtonScript.dragonID && DragonShop.dragonShop.dragonList[j].bought && DragonShop.dragonShop.dragonList[j].dragonID != dragonID)
    63.                 {
    64.                     buyButtonScript.buttonText.text = "Use";
    65.                 }
    66.             }
    67.         }
    68.         */
    69.         DragonShop.dragonShop.UpdateBuyButtons();
    70.         DragonShop.dragonShop.UpdateSprite(dragonID);
    71.  
    72.     }
    73. }
    74.  
     
  2. Primoz56

    Primoz56

    Joined:
    May 9, 2018
    Posts:
    369
    You should be able to store your 'selected' or bought character somewhere that every object can access, such as your player object or an Instanced object. After that you have code relevant to that character work as expected.

    You can also use the UpdateSprite and similar functions after switching/purchasing to just update all relevant objects to their new counterparts. (alternative)

    If the issue is how can you swap characters after buying them, then you just need a character selection object/screen.