Search Unity

Problems retrieving an item ID in the Grid

Discussion in 'Scripting' started by Danoninha, Jul 28, 2020.

  1. Danoninha

    Danoninha

    Joined:
    Mar 17, 2020
    Posts:
    22
    Hello guys! So ... I had some problems with the grid and the flow of my code and I came to ask for your help.

    Last night I started working on a project and tried to make a menu with scroll view and grid, I created a button for each category and this button calls a grid-controlled prefab (see the image to understand better), in theory I am trying to retrieve the id of the menu items and clicking on them makes them visible on the screen, in theory until it works, but I'm having trouble putting it into practice ... any suggestions?



    This is code that I started to make, I don't know if it's too early to ask for help, but I'm having problems with that...
    If you have any suggestions on how this can work better I would be very happy!! :)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Skincontroller : MonoBehaviour
    7. {
    8.  
    9.     public GameObject panelcontrol;
    10.     public Image prefabcontrol;
    11.  
    12.     public Sprite[] skintone = new Sprite [7];
    13.  
    14.     void Start()
    15.     {
    16.  
    17.  
    18.         foreach (Transform child in panelcontrol.transform)
    19.         {
    20.             GameObject.Destroy(child.gameObject);
    21.         }
    22.  
    23.         skintone = Resources.LoadAll<Sprite>("Icones/SknIcon");
    24.  
    25.         for (int qntdskin = 0; qntdskin < skintone.Length; qntdskin++)
    26.         {
    27.  
    28.             prefabcontrol = Instantiate(prefabcontrol, new Vector2(0, 0), Quaternion.identity, panelcontrol.transform);
    29.             prefabcontrol.sprite = skintone[qntdskin];
    30.         }
    31.     }
    32.  
    This is the section that controls the call:

    Code (CSharp):
    1.     public void LoadSkin()
    2.     {
    3.  
    4.         GetComponent<Skincontroller>().enabled = true;
    5.  
    6.     }
     
  2. AnthonySharp

    AnthonySharp

    Joined:
    Apr 11, 2017
    Posts:
    88
    I'm not 100% sure what it is you're stuck with ... but it looks like what you're trying to achieve is to have separate panels (e.g. for skin colour, eyebrows, eyes etc), and then each panel has multiple options you can choose from (e.g. white skin, brown skin, green skin [:D]).

    I'm also guessing you want it to be dynamically loading and not hard-coded (so e.g. you can just create a new icon in Icones/SknIcon and a new button will appear and work).

    What I would do is something like this:
    • Create a prefab with a class called SkinButton.
    • On loading the skin colour panel, the SkinController would load the Icones/SknIcon folder, and for each icon it found, it would instantiate a different button (the SkinButton prefab) inside the grid, also passing to the button a reference to its own texture.
    • In every button's onpress event would be a function like OnButtonPressed(), which would call something like SkinController.OnPressSkinChoice(Texture2D skintexture), passing that button's skin colour to the SkinController.
    • The SkinController would then set the skin colour of the character.
    Not sure if that is helpful but feel free to ask if you get stuck.
     
  3. Danoninha

    Danoninha

    Joined:
    Mar 17, 2020
    Posts:
    22

    Thankss!!! *-*
    Well, I tested your instructions and it worked beautifully!
    But now I'm having trouble resetting the prefab, let's say I can only open the category once and then I can't go back to the previous one.
    But at least the character already changes clothes haha
    It is a pity that I am facing this new problem.