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

sprites arrays

Discussion in 'Scripting' started by Deleted User, Jul 8, 2018.

  1. Deleted User

    Deleted User

    Guest

    in this NewBoss script there is 20 panels in game scene.
    there is also 20 sprites in this script and i set them in the game scene too.

    each time that game runs , every panel will show one of those sprites randomly.
    that means there are 20 panels , there are 20 sprites , so every panel will show 1 sprites. but each time game runs each panel shows a different sprite.

    however , now i have got 20 sprites more.now totally i have got 40 sprites. but still 20 panels. panel will stay 20 always.

    can someone please tell me how to add these new 20 sprites to the rest and totally to have 40 sprites in code?
    so that after that , each panel randomly shows from 40 sprites rather than 20.
    here is my script. thanks.
    Code (CSharp):
    1. public class NewBoss : MonoBehaviour
    2. {
    3.     public List<Sprite> m_AllItems = new List<Sprite>();
    4.     public RectTransform[] m_AllPanels;
    5.     public RectTransform[] m_IngameItems;
    6.  
    7.  
    8.     void Start()
    9.     {
    10.         m_ItemContainer.gameObject.SetActive(false);
    11.  
    12.     }
     
  2. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    449
    In Editor select the GameObject that has the NewBoss Script. Toggle the little lock symbol on top, so that it stays selected in the inspector. Select the new sprites you want to use. Drag them onto the m_AllItems List. Now the List contains the previously added sprites and the newly added sprites. Your code does not show the relevant parts, to be honest. But I guess it will just randomly pick an item from the m_AllItems-List.
     
    Deleted User likes this.
  3. FernandoHC

    FernandoHC

    Joined:
    Feb 6, 2018
    Posts:
    333
    Your code is generating a random number from 0 to the total number of isntantitated objects of m_AllItems at
    int nextItem = Random.Range(0, m_AllItems.Count);
    So there is nothing to fix as far as I can see, just make sure to check at runtime if your object really have the 40 objects assigned to it in the inspector.
     
  4. FernandoHC

    FernandoHC

    Joined:
    Feb 6, 2018
    Posts:
    333
    Sorry but I'm having a bit of a hard time understanding what's going on exactly. Could you make a video and try to put in simple words what's expected and what's happening?
     
  5. Deleted User

    Deleted User

    Guest

    so i change the way i have questioned . i hope this one make senses to you!
    there is a list in which there are 20 member. this is called all items.
    there is an array of panels which has also 20 member. this is all panels.

    there is another array which has 20 member and referes to thumbnail picture of the list. this is called in game items A.
    now i have created a new array for in game items . it is called in game items B.

    how can i say in the code , sometimes randomly choose and show from in game itemsA and sometimes from in game itemsB?
    and in the future from array in game items C and...
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class NewBoss : MonoBehaviour
    7. {
    8.     public List<Sprite> m_AllItems = new List<Sprite>();
    9.     public RectTransform[] m_AllPanels;
    10.     public RectTransform[] m_IngameItems;
    11.     public GameObject m_ItemContainer;
    12.     public float m_DisplayTime = 5.0f;
    13.     public float m_TimeUntilNextItem = 60.0f;
    14.     public Text m_TextTimer;
    15.     public Image m_ImageItem;
    16.     public int m_MaxRequestedItems = 15;
    17.  
    18.     private Sprite m_currentItem;
    19.     private int m_currentItemIndex;
    20.     private float m_displayTimeLeft;
    21.     private float m_nextItemTimeLeft;
    22.     private bool m_found;
    23.     private bool m_isActive;
    24.     private GameController m_gameController;
    25.     private int m_neededItems = 1;
    26.     private int m_foundItems;
    27.  
    28.     void Start()
    29.     {
    30.         m_ItemContainer.gameObject.SetActive(false);
    31.         m_gameController = GameObject.FindObjectOfType<GameController>();
    32.         m_nextItemTimeLeft = m_TimeUntilNextItem;
    33.     }
    34.  
    35.     void Update()
    36.     {
    37.         if (m_displayTimeLeft < 0 && m_isActive)
    38.         {
    39.             if (!m_found)
    40.             {
    41.                 m_gameController.lives--;
    42.                 m_gameController.livesText.text = m_gameController.lives.ToString();
    43.                 m_gameController.livesimage.gameObject.GetComponent<Animator>().SetBool("ToLivesIcon", true);
    44.                 m_ItemContainer.gameObject.SetActive(false);
    45.                 m_isActive = false;
    46.                 m_gameController.enableThrow = !m_isActive;
    47.                 m_nextItemTimeLeft = m_TimeUntilNextItem;
    48.             }
    49.         }
    50.         if (!m_isActive && m_nextItemTimeLeft < 0)
    51.         {
    52.             m_foundItems = 0;
    53.             DisplayNextItem();
    54.         }
    55.         m_TextTimer.text = (Mathf.CeilToInt(m_displayTimeLeft)).ToString();
    56.         m_displayTimeLeft -= Time.deltaTime;
    57.         m_nextItemTimeLeft -= Time.deltaTime;
    58.     }
    59.  
    60.     private void DisplayNextItem()
    61.     {
    62.         int nextItem = Random.Range(0, m_AllItems.Count);
    63.         if (nextItem == m_currentItemIndex)
    64.         {
    65.             nextItem = (nextItem + 1) % m_AllItems.Count;
    66.         }
    67.         m_currentItemIndex = nextItem;
    68.         m_currentItem = m_AllItems[m_currentItemIndex];
    69.         m_ImageItem.sprite = m_currentItem;
    70.         m_displayTimeLeft = m_DisplayTime;
    71.         m_TextTimer.text = ((int)m_displayTimeLeft).ToString();
    72.         m_ItemContainer.gameObject.SetActive(true);
    73.         m_found = false;
    74.         m_isActive = true;
    75.         m_gameController.enableThrow = !m_isActive;
    76.         ShuffleItems();
    77.     }
    78.      
    79.      
    80.     private void ShuffleItems()
    81.     {
    82.         List<RectTransform> allItems = new List<RectTransform>(m_IngameItems);
    83.         List<RectTransform> allPanels = new List<RectTransform>(m_AllPanels);
    84.         while (allPanels.Count > 0 && allItems.Count > 0)
    85.         {
    86.             int spriteIndex = Random.Range(0, allItems.Count);
    87.             int panelIndex = Random.Range(0, allPanels.Count);
    88.             RectTransform item = allItems[spriteIndex];
    89.             RectTransform panel = allPanels[panelIndex];
    90.             item.localPosition = panel.localPosition;
    91.             allItems.Remove(item);
    92.             allPanels.Remove(panel);
    93.         }
    94.     }
    95.  
    96.     public void SelectItem(int _index)
    97.     {
    98.         if (_index == m_currentItemIndex)
    99.         {
    100.             m_found = true;
    101.             m_ItemContainer.gameObject.SetActive(false);
    102.             m_isActive = false;
    103.             m_gameController.enableThrow = !m_isActive;
    104.             m_nextItemTimeLeft = m_TimeUntilNextItem;
    105.  
    106.             ShopScript.money += 1;
    107.             persistentData.Save();
    108.             PlayerInput.instance.currentScore++;
    109.             m_foundItems++;
    110.             if (m_neededItems > m_foundItems)
    111.             {
    112.                 DisplayNextItem();
    113.             }
    114.             else
    115.             {
    116.                 if (m_neededItems < m_MaxRequestedItems)
    117.                 {
    118.                     m_neededItems++;
    119.                 }
    120.             }
    121.         }
    122.         else
    123.         {
    124.             m_displayTimeLeft -= 2.0f;
    125.         }
    126.     }
    127. }
    128.  
     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,841
    Wouldn't that just be something like:

    Code (csharp):
    1. if (Random.Range(0,100) < 50) {
    2.    // pick from itemsA
    3. } else {
    4.    // pick from itemsB
    5. }
     
  7. Deleted User

    Deleted User

    Guest

    can be, as i am so beginner, would you please take a look at that class and specify how can i make that panel length change? if somewhere you have question , please let me know it . thanks.
     
  8. Deleted User

    Deleted User

    Guest

    in a very simple way :
    you had 20 sprites. you have 20 panels. each panels show randomly one sprite.
    now you have 40 sprites. you have 20 panels. how randomly each time , each panel a sprite from those 40 sprites?
     
  9. Deleted User

    Deleted User

    Guest

    please help me !
     
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Can you pick the same sprite twice or no?

    If yes, just pick one and be done with it.

    If no, then I'd duplicate your list, and each time you get a sprite to show, remove it from the duplicated list.
    That's it. :)
     
    Deleted User likes this.
  11. Deleted User

    Deleted User

    Guest

    the best solution ! thanks!