Hello, i have a strange problem
When i assign a var to a list (category) and then other var to other list (clothes) the first one changes suddenly without any code to the value of the second.
See the screenshot to better understand.
thank you
Hello, i have a strange problem
When i assign a var to a list (category) and then other var to other list (clothes) the first one changes suddenly without any code to the value of the second.
See the screenshot to better understand.
thank you
Please post more code related to catlistsorted(with code tags, not pictures)… There are no problems that I can see with the current code.
ok here is the part where i reorder the database with lists.
public void actualizarbasededatos()// database to lists to clean blank slots when sorted button is pushed. Example: database 10 slots, 2 are objects in slots 2 and 6, just reorder the database to place objects
// in slot 0 and 1 and the rest blank slots.
{
int a = 0;
for (int i = 0; i < GetComponent<JugadorBD2>().objetosMochila.Count; i++)
{
if (jugadorBD.objetosMochila[i].iD != 0) // look for the objects not blank (2 and 6 in our example) and assign them to slots 0 and 1.
{
// *******************************************************PROBLEM************************************************************************************************************
catlistsorted[a].text = GetComponent<JugadorBD2>().objetosMochila[i].thecategory; // assigns thecategory var to catlistsorted
Debug.Log(System.DateTime.Now.ToLongTimeString() + " -------------------------------catlistsorted[a].text = " + catlistsorted[a].text + "------------- correct");
ClothListsorted[a].text = GetComponent<JugadorBD2>().objetosMochila[i].thecloth; // assigns thecloth var to clothlistsorted
Debug.Log(System.DateTime.Now.ToLongTimeString() + " -----------------------------ClothListsorted[a].text = " + ClothListsorted[a].text + "------------- correct");
// *** check again category catlistsorted and the value change to the clothlistsorted without code it ***********
Debug.Log(System.DateTime.Now.ToLongTimeString() + " -------------------------------catlistsorted[a].text = " + catlistsorted[a].text + "----- Error(must be Clothes)");
// ***************************************************PROBLEM**************************************************************************************************************************
idListsorted[a]= GetComponent<JugadorBD2>().objetosMochila[i].iD; // assigned correctly to the sortedlists
cantidadListsorted[a] = GetComponent<JugadorBD2>().objetosMochila[i].cantidadSlot; // assigned correctly to the sortedlists
idslotListsorted[a]= a; // assigned correctly to the sortedlists
usableListsorted[a] = GetComponent<JugadorBD2>().objetosMochila[i].sepuedeusar; // assigned correctly to the sortedlists
pesoListsorted[a] = GetComponent<JugadorBD2>().objetosMochila[i].peso; // assigned correctly to the sortedlists
iconoMochilaListsorted[a].sprite = GetComponent<JugadorBD2>().objetosMochila[i].iconobj; // assigned correctly to the sortedlists
a = a + 1;
}
}
for (int i = a; i <GetComponent<JugadorBD2>().objetosMochila.Count; i++) // slot 2 to 10 blank or nulls
{
idListsorted[a]= 0;
cantidadListsorted[a] = 0;
idslotListsorted[a]= a;
catlistsorted[a].text = "";
usableListsorted[a] = true;
ClothListsorted[a].text ="";
pesoListsorted[a] =0;
iconoMochilaListsorted[a].sprite=null;
}
for (int i = 0; i <GetComponent<JugadorBD2>().objetosMochila.Count; i++)//redo database without blank slots so its ready to show in inventory
{
jugadorBD.objetosMochila[i].IDslot = idslotListsorted[i];
jugadorBD.objetosMochila[i].iD = idListsorted[i];
jugadorBD.objetosMochila[i].thecategory =catlistsorted[i].text;
jugadorBD.objetosMochila[i].cantidadSlot= cantidadListsorted[i];
jugadorBD.objetosMochila[i].sepuedeusar = usableListsorted[i];
jugadorBD.objetosMochila[i].thecloth = ClothListsorted[i].text;
jugadorBD.objetosMochila[i].peso = pesoListsorted[i];
jugadorBD.objetosMochila[i].iconobj = iconoMochilaListsorted[i].sprite;
}
}
The lists
[HideInInspector]public List<Image> slotList = new List<Image>();
[HideInInspector]public List<Image> iconoList = new List<Image>();
[HideInInspector]public List<Text> textList = new List<Text>();
[HideInInspector]public List<Image> slotMochilaList = new List<Image>();
[HideInInspector]public List<Image> iconoMochilaList = new List<Image>();
[HideInInspector]public List<Text> textMochilaList = new List<Text>();
[HideInInspector]public List<Button> butonmochilausarList = new List<Button>();
[HideInInspector]public List<Button> butonmochilatirarList = new List<Button>();
[HideInInspector]public List<Image> iconoMochilaListsorted = new List<Image>();
[HideInInspector]public List<int> idslotListsorted = new List<int>();
[HideInInspector]public List<Text> catlistsorted = new List<Text>();
[HideInInspector]public List<int> idListsorted = new List<int>();
[HideInInspector]public List<int> cantidadListsorted = new List<int>();
[HideInInspector]public List<bool> usableListsorted = new List<bool>();
[HideInInspector]public List<Text> ClothListsorted = new List<Text>();
[HideInInspector]public List<int> pesoListsorted = new List<int>();
Items added via inspector, here the class
using UnityEngine;
using System.Collections.Generic;
[ExecuteInEditMode]
public class Objetos : MonoBehaviour {
[SerializeField]public List<Objeto> objeto;
[System.Serializable]public class Objeto{
public string nombre;
public Sprite icono;
public GameObject prefab;
//public enum Category {Ninguna,Salud,Viveres,Ropa,Utensilios,Crafteable};
public enum Category {Nothing,Health,Food,Clothes,Tools,Craft};
public Category category;
public enum Cloth {Nothing,Head,Glasses,Body,Legs,Shoes};
public Cloth cloth;
public int puntosQueAfecta ;
public bool sePuedeStackear;
public int maxStack;
public bool sePuedeUsar ;
public Sprite fichaitem;
public int pesoDelObjeto;
[HideInInspector]
[Range(1, 10)]
public int dificultadParaEncontrarlo=1;
[HideInInspector]public int iD = 0;
}
void Awake()
{
SetID();
}
void SetID()
{
for(int i = 0; i < objeto.Count; i++)
{
objeto[i].iD = i;
}
}
public void Update()
{
Objeto objetoVacio=new Objeto();
objetoVacio.nombre = "Vacio";
objetoVacio.icono = null;
objetoVacio.prefab = null;
objetoVacio.iD = 0;
objetoVacio.fichaitem = null;
objetoVacio.pesoDelObjeto = 0;
objetoVacio.puntosQueAfecta = 0;
objetoVacio.sePuedeStackear = false;
objetoVacio.sePuedeUsar = false;
objetoVacio.maxStack = 0;
if (objeto.Count == 0)
objeto.Add(objetoVacio);
else
objeto[0] = objetoVacio;
}
}
Database list
using UnityEngine;
using System.Collections.Generic;
public class JugadorBD2 : MonoBehaviour {
[HideInInspector]
[SerializeField]public List<ObjetosEnMochila> objetosMochila = new List<ObjetosEnMochila>();
[System.Serializable]public class ObjetosEnMochila
//[System.NonSerialized]public class ObjetosEnMochila
{
public int IDslot = 0;
public int iD = 0;
public int cantidadSlot = 0;
public string thecategory="" ;
public string thecloth ="";
public bool sepuedeusar=false;
public int peso = 0 ;
public Sprite iconobj = null;
}
void Start() {
ProceduralSlots proceduralSlots = GetComponent<ProceduralSlots>();
ListCapacityMochila(proceduralSlots);
}
void Update() {
}
void ListCapacityMochila(ProceduralSlots slots)
{
for (int i = 0; i < (slots.filasMochila*slots.columnasMochila); i++)
{
objetosMochila.Add(new ObjetosEnMochila());
}
}
}
And here is the way when the player picks the object is added to the database
{
if (jugadorBD.objetosMochila[i].iD == 0) // blank slot, then add the item
{
jugadorBD.objetosMochila[i].IDslot = i;
jugadorBD.objetosMochila[i].iD = name.iD;
jugadorBD.objetosMochila[i].thecloth = objetos.objeto[name.iD].cloth.ToString();
jugadorBD.objetosMochila[i].thecategory= objetos.objeto[name.iD].category.ToString();
jugadorBD.objetosMochila[i].iconobj = objetos.objeto[name.iD].icono;
jugadorBD.objetosMochila[i].sepuedeusar = objetos.objeto[name.iD].sePuedeUsar;
jugadorBD.objetosMochila[i].peso = objetos.objeto[name.iD].pesoDelObjeto;
Destroy(hit.collider.gameObject);
jugadorBD.objetosMochila[i].cantidadSlot += stack.Stack;
haySlotsVaciosMochila = false;
if (recogerSonido)
cam.gameObject.GetComponent<AudioSource>().PlayOneShot(recogerSonido);
else
Debug.Log("Podrias agregar un sonido");
return;
}
}
The mess began when i added a new object in the database for the game,because before that it works correctly.May be some memory mess, but i have reset the component, delete the script and redo again, … nothing works
It looks like catlistsorted[a] and ClothListsorted[a] are both referencing the same Text object. I don’t really understand why you are storing nulled references in your database when you have ID’s available. You could just store the objects in a dictionary with <id, object> and then iterate the elements. This would remove the need to re-sort a list when elements are removed. Also, are you aware your Objeto class is creating new instances every frame???
the courious is that the catlistsorted[a] is assigned correctly and when clothlistadortes[a] is assigned the value of catlistsorted[a] changes suddenly.I deleted the clothlistsorted from the game and the catlistsorted works correcly all the time.Then if i add a new field for cloth with a new list for cloth with different names , the value of the catlistsorted changes again.i am thinking if can be a prefab problem,a memory allocation problem,cache i dont know really because the rest of the object fields are assigned correctly
I feel like I’m contributing to a disaster… Anyway, you need to post the code that is assigning the Text object to your catlistsorted list and ClothListsorted list.
Thanks jim for your help and may be you are right.I am a newbie in C and i am adapting an inventory project i found with crafting and building to my needs , but is possible its not as perfect as i thought.
The code assigning the values to the sorted lists its in the first box code here at lines 16 and 20.
This is the process, if i understood it correctly.
The items to the database (objetos) are added via inspector:
When the player picks the object, here is the code(box 4 ) but i put again here
for (int i = 0; i < jugadorBD.objetosMochila.Count; i++)
{
if (jugadorBD.objetosMochila[i].iD == 0) // blank slot, then add the item
{
jugadorBD.objetosMochila[i].IDslot = i;
jugadorBD.objetosMochila[i].iD = name.iD;
jugadorBD.objetosMochila[i].thecloth = objetos.objeto[name.iD].cloth.ToString();
jugadorBD.objetosMochila[i].thecategory= objetos.objeto[name.iD].category.ToString();
jugadorBD.objetosMochila[i].iconobj = objetos.objeto[name.iD].icono;
jugadorBD.objetosMochila[i].sepuedeusar = objetos.objeto[name.iD].sePuedeUsar;
jugadorBD.objetosMochila[i].peso = objetos.objeto[name.iD].pesoDelObjeto;
Destroy(hit.collider.gameObject);
jugadorBD.objetosMochila[i].cantidadSlot += stack.Stack;
haySlotsVaciosMochila = false;
if (recogerSonido)
cam.gameObject.GetComponent<AudioSource>().PlayOneShot(recogerSonido);
else
Debug.Log("Podrias agregar un sonido");
return;
}
}
Thed, from here, when i open the inventory, the actualizarbasededatos() function is called and begins the database sorting if there is any empty slot.Here is the assigment to the sorted lists.
public void actualizarbasededatos()// database to lists to clean blank slots when sorted button is pushed. Example: database 10 slots, 2 are objects in slots 2 and 6, just reorder the database to place objects
// in slot 0 and 1 and the rest blank slots.
{
int a = 0;
for (int i = 0; i < GetComponent<JugadorBD2>().objetosMochila.Count; i++)
{
if (jugadorBD.objetosMochila[i].iD != 0) // look for the objects not blank (2 and 6 in our example) and assign them to slots 0 and 1.
{
// *******************************************************PROBLEM************************************************************************************************************
catlistsorted[a].text = GetComponent<JugadorBD2>().objetosMochila[i].thecategory; // assigns thecategory var to catlistsorted
Debug.Log(System.DateTime.Now.ToLongTimeString() + " -------------------------------catlistsorted[a].text = " + catlistsorted[a].text + "------------- correct");
ClothListsorted[a].text = GetComponent<JugadorBD2>().objetosMochila[i].thecloth; // assigns thecloth var to clothlistsorted
Debug.Log(System.DateTime.Now.ToLongTimeString() + " -----------------------------ClothListsorted[a].text = " + ClothListsorted[a].text + "------------- correct");
// *** check again category catlistsorted and the value change to the clothlistsorted without code it ***********
Debug.Log(System.DateTime.Now.ToLongTimeString() + " -------------------------------catlistsorted[a].text = " + catlistsorted[a].text + "----- Error(must be Clothes)");
// ***************************************************PROBLEM**************************************************************************************************************************
idListsorted[a]= GetComponent<JugadorBD2>().objetosMochila[i].iD; // assigned correctly to the sortedlists
cantidadListsorted[a] = GetComponent<JugadorBD2>().objetosMochila[i].cantidadSlot; // assigned correctly to the sortedlists
idslotListsorted[a]= a; // assigned correctly to the sortedlists
usableListsorted[a] = GetComponent<JugadorBD2>().objetosMochila[i].sepuedeusar; // assigned correctly to the sortedlists
pesoListsorted[a] = GetComponent<JugadorBD2>().objetosMochila[i].peso; // assigned correctly to the sortedlists
iconoMochilaListsorted[a].sprite = GetComponent<JugadorBD2>().objetosMochila[i].iconobj; // assigned correctly to the sortedlists
a = a + 1;
}
}
for (int i = a; i <GetComponent<JugadorBD2>().objetosMochila.Count; i++) // slot 2 to 10 blank or nulls
{
idListsorted[a]= 0;
cantidadListsorted[a] = 0;
idslotListsorted[a]= a;
catlistsorted[a].text = "";
usableListsorted[a] = true;
ClothListsorted[a].text ="";
pesoListsorted[a] =0;
iconoMochilaListsorted[a].sprite=null;
}
for (int i = 0; i <GetComponent<JugadorBD2>().objetosMochila.Count; i++)//redo database without blank slots so its ready to show in inventory
{
jugadorBD.objetosMochila[i].IDslot = idslotListsorted[i];
jugadorBD.objetosMochila[i].iD = idListsorted[i];
jugadorBD.objetosMochila[i].thecategory =catlistsorted[i].text;
jugadorBD.objetosMochila[i].cantidadSlot= cantidadListsorted[i];
jugadorBD.objetosMochila[i].sepuedeusar = usableListsorted[i];
jugadorBD.objetosMochila[i].thecloth = ClothListsorted[i].text;
jugadorBD.objetosMochila[i].peso = pesoListsorted[i];
jugadorBD.objetosMochila[i].iconobj = iconoMochilaListsorted[i].sprite;
}
}
it looks the value in :
GetComponent().objetosMochila*.thecategory and stores in catlistsorted[a]*
GetComponent().objetosMochila*.thecloth and stores in Clothlistsorted[a]*
thecategory and thecloth fields have correct values, they assign well to the lists , but at the moment the Clothlistsorted[a] is assignes, the value of catlistsorted[a] changes suddenly.If i dont use the Clothlistsorted[a] it does not change.Look the screenshot, is debugged correctly just a millisecond before.
Hope i have explained i bit better now but i am afraid that my english is as good as my C skills .Sorry
I need to know where you are adding the Text object references to the lists…
[HideInInspector]public List<Text> catlistsorted = new List<Text>();
[HideInInspector]public List<Text> ClothListsorted = new List<Text>();
catlistsorted.Add(new Text());//Where are you doing this?
The Text objects also seem completely unnecessary. Modify your lists to use strings.
[HideInInspector]public List<string> catlistsorted = new List<string>();
[HideInInspector]public List<string> ClothListsorted = new List<string>();
Also please cache the result of GetComponent at the beginning of your method instead of using multiple GetComponent calls inside of your loop.
Example:
public void actualizarbasededatos()
{
JugadorBD2 jugadorBD = GetComponent<JugadorBD2>();
int a = 0;
for (int i = 0; i < jugadorBD.objetosMochila.Count; i++)
{
//...
catlistsorted[a] = jugadorBD.objetosMochila[i].thecategory;
This is also VERY BAD. You are creating an instance of Objecto EVERY frame and discarding the old one.
public void Update()
{
Objeto objetoVacio = new Objeto();
objetoVacio.nombre = "Vacio";
objetoVacio.icono = null;
objetoVacio.prefab = null;
objetoVacio.iD = 0;
objetoVacio.fichaitem = null;
objetoVacio.pesoDelObjeto = 0;
objetoVacio.puntosQueAfecta = 0;
objetoVacio.sePuedeStackear = false;
objetoVacio.sePuedeUsar = false;
objetoVacio.maxStack = 0;
if (objeto.Count == 0)
objeto.Add(objetoVacio);
else
objeto[0] = objetoVacio;
}
Thank you very much for the suggestions.I will correct them.
I attach the class.Lines 133,134.Its the text from the slot, because its only used one time.When the player picks something the list are regenerated.
using UnityEngine;
using System.Collections.Generic;
using System.Collections;
using UnityEngine.UI;
public class ProceduralSlots : MonoBehaviour {
[Header("Elementos")]
public GameObject panelInv;
public GameObject canvas;
public GameObject slotContainer;
public GameObject mochilaContainer;
public GameObject sslots;
public GameObject prefabbutton;
public GameObject prefabbuttontirar;
public Sprite defaultempty;
[Header("Slots")]
public bool HotbarActiva = true;
public int cantidadDeSlots;
public float alturaSlots;
public bool slotOutline = true;
[Range(10, 75)]public float tamaño;
[Header("Iconos")]
[Range(2, 20)]public float offsetIcono;
public bool desactivarIconoVacio;
public bool iconoOutline;
[Header("Texto Stackear")]
public float tamañoTranform = 10f;
public float separacionDelSlotX;
public float separacionDelSlotY;
public Font fuenteDelTexto;
public int tamañoDeLetra = 14;
public bool textOutline = true;
[HideInInspector]
public bool debugInUpdate = false;
[Header("Mochila")]
public int filasMochila;
public int columnasMochila;
public float alturaMochila;
public Vector2 offsetFondoMin = new Vector2();
public Vector2 offsetFondoMax = new Vector2();
public Vector2 positionSlotsMochila = new Vector2();
public Vector2 tamañoSlotsMochila = new Vector2();
public Vector2 slotMochilaSeparacion = new Vector2();
private int startSlots;
[SerializeField]Colores color;
[HideInInspector]public List<Image> slotList = new List<Image>();
[HideInInspector]public List<Image> iconoList = new List<Image>();
[HideInInspector]public List<Text> textList = new List<Text>();
[HideInInspector]public List<Image> slotMochilaList = new List<Image>();
[HideInInspector]public List<Image> iconoMochilaList = new List<Image>();
[HideInInspector]public List<Text> textMochilaList = new List<Text>();
[HideInInspector]public List<Button> butonmochilausarList = new List<Button>();
[HideInInspector]public List<Button> butonmochilatirarList = new List<Button>();
[HideInInspector]public List<Image> iconoMochilaListsorted = new List<Image>();
[HideInInspector]public List<int> idslotListsorted = new List<int>();
[HideInInspector]public List<Text> catlistsorted = new List<Text>();
[HideInInspector]public List<int> idListsorted = new List<int>();
[HideInInspector]public List<int> cantidadListsorted = new List<int>();
[HideInInspector]public List<bool> usableListsorted = new List<bool>();
[HideInInspector]public List<Text> ClothListsorted = new List<Text>();
[HideInInspector]public List<int> pesoListsorted = new List<int>();
private InventarioManager inventario;
private JugadorBD2 jugadorBD;
private Objetos objetosBD;
[HideInInspector]public DragInfo1 itemBeingDrag;
int slotpointedantiguo = 0;
private GameObject boton;
private GameObject botonusar;
private GameObject botontirar;
private AbrirCofreInv cofreinv;
public string botonpulsado = "All";
selectslot slotsel;
void Start()
{
startSlots = cantidadDeSlots;
inventario = GetComponent<InventarioManager>();
cofreinv = GetComponent<AbrirCofreInv>();
jugadorBD = GetComponent<JugadorBD2>();
objetosBD = GetComponent<Objetos>();
slotsel = GetComponent<selectslot>();
GameObject dragImage = Instantiate(new GameObject(), Vector3.zero, Quaternion.identity) as GameObject;
dragImage.transform.parent = canvas.transform;
dragImage.name = "DragImage";
Image imageDrag =dragImage.AddComponent<Image>();
// if(imageDrag){}
RectTransform transformDrag = dragImage.GetComponent<RectTransform>();
transformDrag.sizeDelta = new Vector2(tamaño, tamaño);
transformDrag.anchorMin = new Vector2(0.5f, 0);
transformDrag.anchorMax = new Vector2(0.5f, 0);
dragImage.AddComponent<DragInfo1>();
dragImage.AddComponent<CanvasGroup>();
itemBeingDrag = dragImage.GetComponent<DragInfo1>();
itemBeingDrag.GetComponent<Image>().color = color.objetoArrastrado;
for (int j = 0; j < filasMochila; j++)//SlotsMochila
{
for (int i = 0; i < columnasMochila; i++)
{
GameObject slot = (GameObject)Instantiate(sslots);
slot.transform.SetParent(mochilaContainer.transform,false);
slot.name = "Slot" + (i + columnasMochila * j).ToString();
Image imageSlot = slot.AddComponent<Image>();
Image imageIcono = slot.transform.GetChild(0).GetComponent<Image>();
SlotInfo slotInfo = slot.gameObject.AddComponent<SlotInfo>();
slotInfo.itemBeingDrag = dragImage;
slotInfo.tipoInv = 1;
slot.GetComponent<SlotInfo>().numSlot = (i + columnasMochila * j);
slotMochilaList.Add(imageSlot);
iconoMochilaList.Add(imageIcono);
iconoMochilaListsorted.Add(imageIcono);
Text text= slot.transform.GetChild(1).GetComponent<Text>();
textMochilaList.Add(text);
catlistsorted.Add(text);
ClothListsorted.Add(text);
idslotListsorted.Add(0);
idListsorted.Add(0);
cantidadListsorted.Add(0);
usableListsorted.Add(true);
pesoListsorted.Add(0);
Button buttonusar = slot.transform.GetChild(2).GetComponent<Button>();
buttonusar.onClick.AddListener(() => inventario.UsarItem());
Button buttontirar = slot.transform.GetChild(3).GetComponent<Button>();
buttontirar.onClick.AddListener(() => inventario.Drop());
}
}
DestruirGameObjectsVacios();
}
void Update()
{
panelInv.gameObject.SetActive(GetComponent<InventarioManager>().mochilaTrigger);
}
public void DestruirGameObjectsVacios()
{
GameObject[] go =FindObjectsOfType<GameObject>();
foreach(GameObject g in go)
{
if (g.name == "New Game Object")
{
Destroy(g);
}
}
}
public void SlotsUI( )
{
desactivarbotonesusartirar();
if (botonpulsado == "All")
{
for (int i = 0; i < iconoMochilaList.Count; i++)
{
iconoMochilaList[i].sprite = jugadorBD.objetosMochila[i].iconobj;
textMochilaList[i].text = jugadorBD.objetosMochila[i].cantidadSlot.ToString();
}
for (int i = 0; i < iconoMochilaList.Count; i++)
{
//if (!objetosBD.objeto[jugadorBD.objetosMochila[i].iD].sePuedeStackear || jugadorBD.objetosMochila[i].iD == 0)//Texto
if (GetComponent<JugadorBD2>().objetosMochila[i].iD == 0)
{
iconoMochilaList[i].enabled = false;
textMochilaList[i].enabled = false;
}
else
{
iconoMochilaList[i].enabled = true;
textMochilaList[i].enabled = true;
}
slotMochilaList[i].color = color.slotActivo;
}
}
else if (botonpulsado == "Salud")
{
//
for (int i = 0; i < iconoMochilaList.Count; i++)//SlotsMochila
{
iconoMochilaList[i].sprite = iconoMochilaListsorted[i].sprite ;
textMochilaList[i].text = cantidadListsorted[i].ToString();
if (iconoMochilaList[i].sprite == defaultempty )//|| GetComponent<JugadorBD2>().objetosMochila[i].iD == 0
{
iconoMochilaList[i].enabled = false;
textMochilaList[i].enabled = false;
}
else
{
iconoMochilaList[i].enabled = true;
textMochilaList[i].enabled = true;
}
slotMochilaList[i].color = color.slotActivo;
}
}
else if (botonpulsado == "Viveres")
{
for (int i = 0; i < iconoMochilaList.Count; i++)//SlotsMochila
{
iconoMochilaList[i].sprite = iconoMochilaListsorted[i].sprite ;
//textMochilaList[i].text = textMochilaListsorted[i].text;
textMochilaList[i].text = cantidadListsorted[i].ToString();
//if (!objetosBD.objeto[jugadorBD.objetosMochila[i].iD].sePuedeStackear || jugadorBD.objetosMochila[i].iD == 0)//Texto
if (iconoMochilaList[i].sprite == defaultempty )//|| GetComponent<JugadorBD2>().objetosMochila[i].iD == 0
{
iconoMochilaList[i].enabled = false;
textMochilaList[i].enabled = false;
}
else
{
iconoMochilaList[i].enabled = true;
textMochilaList[i].enabled = true;
}
slotMochilaList[i].color = color.slotActivo;
}
}
else if (botonpulsado == "Ropa")
{
for (int i = 0; i < iconoMochilaList.Count; i++)//SlotsMochila
{
iconoMochilaList[i].sprite = iconoMochilaListsorted[i].sprite ;
// textMochilaList[i].text = textMochilaListsorted[i].text;
textMochilaList[i].text = cantidadListsorted[i].ToString();
//if (!objetosBD.objeto[jugadorBD.objetosMochila[i].iD].sePuedeStackear || jugadorBD.objetosMochila[i].iD == 0)//Texto
if (iconoMochilaList[i].sprite == defaultempty )//|| GetComponent<JugadorBD2>().objetosMochila[i].iD == 0
{
iconoMochilaList[i].enabled = false;
textMochilaList[i].enabled = false;
}
else
{
iconoMochilaList[i].enabled = true;
textMochilaList[i].enabled = true;
}
slotMochilaList[i].color = color.slotActivo;
}
}
else if (botonpulsado == "Utensilios")
{
for (int i = 0; i < iconoMochilaList.Count; i++)//SlotsMochila
{
iconoMochilaList[i].sprite = iconoMochilaListsorted[i].sprite ;
// textMochilaList[i].text = textMochilaListsorted[i].text;
textMochilaList[i].text = cantidadListsorted[i].ToString();
//if (!objetosBD.objeto[jugadorBD.objetosMochila[i].iD].sePuedeStackear || jugadorBD.objetosMochila[i].iD == 0)//Texto
if (iconoMochilaList[i].sprite == defaultempty )//|| GetComponent<JugadorBD2>().objetosMochila[i].iD == 0
{
iconoMochilaList[i].enabled = false;
textMochilaList[i].enabled = false;
}
else
{
iconoMochilaList[i].enabled = true;
textMochilaList[i].enabled = true;
}
slotMochilaList[i].color = color.slotActivo;
}
}
itemBeingDrag.GetComponent<Image>().sprite= GetComponent<Objetos>().objeto[itemBeingDrag.GetComponent<DragInfo1>().ItemID].icono;
}
[System.Serializable]
public class Colores
{
public Color slotActivo = new Color (255,255,255,144);
public Color slotInactivo = new Color (255,255,255,0);
public Color iconoActivo = new Color (203,203,203,95);
public Color iconoInactivo = new Color (255,255,255,144);
public Color textActivo = new Color (203,203,203,95);
public Color textInactivo = new Color (203,203,203,95);
public Color mochilaFondo = new Color (203,203,203,95);
public Color objetoArrastrado = new Color (203,203,203,95);
}
public void botonestodo()
{
actualizarbasededatos();
botonpulsado = "All";
slotsel.botonpulsado = "All";
SlotsUI();
}
public void botonessalud()
{
actualizarbasededatos();
int a = 0;
for (int i = 0; i <GetComponent<JugadorBD2>().objetosMochila.Count; i++)//SlotsMochila
{
if (GetComponent<JugadorBD2>().objetosMochila[i].thecategory == "Salud")
{
Debug.Log("salud");
iconoMochilaListsorted[a].sprite = GetComponent<Objetos>().objeto[GetComponent<JugadorBD2>().objetosMochila[i].iD].icono;
// textMochilaListsorted[a].text = GetComponent<JugadorBD2>().objetosMochila[i].cantidadSlot.ToString();
cantidadListsorted[a] = GetComponent<JugadorBD2>().objetosMochila[i].cantidadSlot;
idslotListsorted[a]= GetComponent<JugadorBD2>().objetosMochila[i].IDslot;
a=a+1;
}
}
for (int i = a; i <GetComponent<JugadorBD2>().objetosMochila.Count; i++)//SlotsMochila
{
iconoMochilaListsorted[i].sprite =defaultempty;
//textMochilaListsorted[i].text = "";
cantidadListsorted[i] = 0;
}
//desactivarbotonesusartirar();
botonpulsado = "Salud";
slotsel.botonpulsado = "Salud";
SlotsUI();
}
public void botonesViveres()
{
actualizarbasededatos();
int a = 0;
for (int i = 0; i <GetComponent<JugadorBD2>().objetosMochila.Count; i++)//SlotsMochila
{
if (GetComponent<JugadorBD2>().objetosMochila[i].thecategory == "Viveres")
{
iconoMochilaListsorted[a].sprite = GetComponent<Objetos>().objeto[GetComponent<JugadorBD2>().objetosMochila[i].iD].icono;
// textMochilaListsorted[a].text = GetComponent<JugadorBD2>().objetosMochila[i].cantidadSlot.ToString();
cantidadListsorted[a] = GetComponent<JugadorBD2>().objetosMochila[i].cantidadSlot;
idslotListsorted[a]= GetComponent<JugadorBD2>().objetosMochila[i].IDslot;
a=a+1;
}
}
for (int i = a; i <GetComponent<JugadorBD2>().objetosMochila.Count; i++)//SlotsMochila
{
iconoMochilaListsorted[i].sprite =defaultempty;
//textMochilaListsorted[i].text = "";
cantidadListsorted[i] = 0;
}
//desactivarbotonesusartirar();
botonpulsado = "Viveres";
slotsel.botonpulsado = "Viveres";
SlotsUI();
}
public void botonesRopa()
{
actualizarbasededatos();
int a = 0;
for (int i = 0; i <GetComponent<JugadorBD2>().objetosMochila.Count; i++)//SlotsMochila
{
if (GetComponent<JugadorBD2>().objetosMochila[i].thecategory == "Ropa")
{
iconoMochilaListsorted[a].sprite = GetComponent<Objetos>().objeto[GetComponent<JugadorBD2>().objetosMochila[i].iD].icono;
//textMochilaListsorted[a].text = GetComponent<JugadorBD2>().objetosMochila[i].cantidadSlot.ToString();
cantidadListsorted[a] = GetComponent<JugadorBD2>().objetosMochila[i].cantidadSlot;
idslotListsorted[a]= GetComponent<JugadorBD2>().objetosMochila[i].IDslot;
a=a+1;
}
}
for (int i = a; i <GetComponent<JugadorBD2>().objetosMochila.Count; i++)//SlotsMochila
{
iconoMochilaListsorted[i].sprite =defaultempty;
//textMochilaListsorted[i].text = "";
cantidadListsorted[i] = 0;
}
//desactivarbotonesusartirar();
botonpulsado = "Ropa";
slotsel.botonpulsado = "Ropa";
SlotsUI();
}
public void botonesUtensilios()
{
actualizarbasededatos();
int a = 0;
for (int i = 0; i <GetComponent<JugadorBD2>().objetosMochila.Count; i++)//SlotsMochila
{
if (GetComponent<JugadorBD2>().objetosMochila[i].thecategory == "Utensilios")
{
iconoMochilaListsorted[a].sprite = GetComponent<Objetos>().objeto[GetComponent<JugadorBD2>().objetosMochila[i].iD].icono;
//textMochilaListsorted[a].text = GetComponent<JugadorBD2>().objetosMochila[i].cantidadSlot.ToString();
cantidadListsorted[a] = GetComponent<JugadorBD2>().objetosMochila[i].cantidadSlot;
idslotListsorted[a]= GetComponent<JugadorBD2>().objetosMochila[i].IDslot;
a=a+1;
}
}
for (int i = a; i <GetComponent<JugadorBD2>().objetosMochila.Count; i++)//SlotsMochila
{
iconoMochilaListsorted[i].sprite =defaultempty;
//textMochilaListsorted[i].text = "";
cantidadListsorted[i] = 0;
}
//desactivarbotonesusartirar();
botonpulsado = "Utensilios";
slotsel.botonpulsado = "Utensilios";
SlotsUI();
}
void desactivarbotonesusartirar()
{
//Debug.Log("proceduralslots");
botonusar = GameObject.Find("botonusar");
botontirar = GameObject.Find("botontirar");
if (botonusar != null )
{
botonusar.SetActive(false);
}
if (botontirar != null )
{
botontirar.SetActive(false);
}
}
public void actualizarbasededatos()// database to lists to clean blank slots when sorted button is pushed. Example: database 10 slots, 2 are objects in slots 2 and 6, just reorder the database to place objects
// in slot 0 and 1 and the rest blank slots.
{
int a = 0;
for (int i = 0; i < GetComponent<JugadorBD2>().objetosMochila.Count; i++)
{
if (jugadorBD.objetosMochila[i].iD != 0) // look for the objects not blank (2 and 6 in our example) and assign them to slots 0 and 1.
{
// *******************************************************PROBLEM************************************************************************************************************
catlistsorted[a].text = GetComponent<JugadorBD2>().objetosMochila[i].thecategory; // assigns thecategory var to catlistsorted
Debug.Log(System.DateTime.Now.ToLongTimeString() + " -------------------------------catlistsorted[a].text = " + catlistsorted[a].text + "------------- correct");
ClothListsorted[a].text = GetComponent<JugadorBD2>().objetosMochila[i].thecloth; // assigns thecloth var to clothlistsorted
Debug.Log(System.DateTime.Now.ToLongTimeString() + " -----------------------------ClothListsorted[a].text = " + ClothListsorted[a].text + "------------- correct");
// *** check again category catlistsorted and the value change to the clothlistsorted without code it ***********
Debug.Log(System.DateTime.Now.ToLongTimeString() + " -------------------------------catlistsorted[a].text = " + catlistsorted[a].text + "----- Error(must be Clothes)");
// ***************************************************PROBLEM**************************************************************************************************************************
idListsorted[a]= GetComponent<JugadorBD2>().objetosMochila[i].iD; // assigned correctly to the sortedlists
cantidadListsorted[a] = GetComponent<JugadorBD2>().objetosMochila[i].cantidadSlot; // assigned correctly to the sortedlists
idslotListsorted[a]= a; // assigned correctly to the sortedlists
usableListsorted[a] = GetComponent<JugadorBD2>().objetosMochila[i].sepuedeusar; // assigned correctly to the sortedlists
pesoListsorted[a] = GetComponent<JugadorBD2>().objetosMochila[i].peso; // assigned correctly to the sortedlists
iconoMochilaListsorted[a].sprite = GetComponent<JugadorBD2>().objetosMochila[i].iconobj; // assigned correctly to the sortedlists
a = a + 1;
}
}
for (int i = a; i <GetComponent<JugadorBD2>().objetosMochila.Count; i++) // slot 2 to 10 blank or nulls
{
idListsorted[a]= 0;
cantidadListsorted[a] = 0;
idslotListsorted[a]= a;
catlistsorted[a].text = "";
usableListsorted[a] = true;
ClothListsorted[a].text ="";
pesoListsorted[a] =0;
iconoMochilaListsorted[a].sprite=null;
}
for (int i = 0; i <GetComponent<JugadorBD2>().objetosMochila.Count; i++)//redo database without blank slots so its ready to show in inventory
{
jugadorBD.objetosMochila[i].IDslot = idslotListsorted[i];
jugadorBD.objetosMochila[i].iD = idListsorted[i];
jugadorBD.objetosMochila[i].thecategory =catlistsorted[i].text;
jugadorBD.objetosMochila[i].cantidadSlot= cantidadListsorted[i];
jugadorBD.objetosMochila[i].sepuedeusar = usableListsorted[i];
jugadorBD.objetosMochila[i].thecloth = ClothListsorted[i].text;
jugadorBD.objetosMochila[i].peso = pesoListsorted[i];
jugadorBD.objetosMochila[i].iconobj = iconoMochilaListsorted[i].sprite;
}
}
}
As I thought… All of your lists are using the same Text object. You will have to use three separate Text components, one for each list.
Text[] textComponents = slot.transform.GetChild(1).GetComponents<Text>();
textMochilaList.Add(textComponents[0]);
catlistsorted.Add(textComponents[1]);
ClothListsorted.Add(textComponents[2]);
Alternatively… and hopefully… You will stop abusing the poor Text component and start using strings.
textMochilaList.Add("");
catlistsorted.Add("");
ClothListsorted.Add("");
thanks again for your pacience.
I modified the list to use with strings.
[HideInInspector]public List<string> catlistsorted = new List<string>();
[HideInInspector]public List<string> ClothListsorted = new List<string>();
private InventarioManager inventario;
private JugadorBD2 jugadorBD;
private Objetos objetosBD;
[HideInInspector]public DragInfo1 itemBeingDrag;
int slotpointedantiguo = 0;
private GameObject boton;
private GameObject botonusar;
private GameObject botontirar;
private AbrirCofreInv cofreinv;
public string botonpulsado = "All";
selectslot slotsel;
void Start()
{
startSlots = cantidadDeSlots;
inventario = GetComponent<InventarioManager>();
cofreinv = GetComponent<AbrirCofreInv>();
jugadorBD = GetComponent<JugadorBD2>();
objetosBD = GetComponent<Objetos>();
slotsel = GetComponent<selectslot>();
GameObject dragImage = Instantiate(new GameObject(), Vector3.zero, Quaternion.identity) as GameObject;
dragImage.transform.parent = canvas.transform;
dragImage.name = "DragImage";
Image imageDrag =dragImage.AddComponent<Image>();
// if(imageDrag){}
RectTransform transformDrag = dragImage.GetComponent<RectTransform>();
transformDrag.sizeDelta = new Vector2(tamaño, tamaño);
transformDrag.anchorMin = new Vector2(0.5f, 0);
transformDrag.anchorMax = new Vector2(0.5f, 0);
dragImage.AddComponent<DragInfo1>();
dragImage.AddComponent<CanvasGroup>();
itemBeingDrag = dragImage.GetComponent<DragInfo1>();
itemBeingDrag.GetComponent<Image>().color = color.objetoArrastrado;
for (int j = 0; j < filasMochila; j++)//SlotsMochila
{
for (int i = 0; i < columnasMochila; i++)
{
GameObject slot = (GameObject)Instantiate(sslots);
slot.transform.SetParent(mochilaContainer.transform,false);
slot.name = "Slot" + (i + columnasMochila * j).ToString();
Image imageSlot = slot.AddComponent<Image>();
Image imageIcono = slot.transform.GetChild(0).GetComponent<Image>();
SlotInfo slotInfo = slot.gameObject.AddComponent<SlotInfo>();
slotInfo.itemBeingDrag = dragImage;
slotInfo.tipoInv = 1;
slot.GetComponent<SlotInfo>().numSlot = (i + columnasMochila * j);
slotMochilaList.Add(imageSlot);
iconoMochilaList.Add(imageIcono);
iconoMochilaListsorted.Add(imageIcono);
Text text= slot.transform.GetChild(1).GetComponent<Text>();
textMochilaList.Add(text);
catlistsorted.Add("");
ClothListsorted.Add("");
Now,i got this.
this is the class, and category and clothes are enums.
using UnityEngine;
using System.Collections.Generic;
[ExecuteInEditMode]
public class Objetos : MonoBehaviour {
[SerializeField]public List<Objeto> objeto;
[System.Serializable]public class Objeto{
public string nombre;
public Sprite icono;
public GameObject prefab;
//public enum Category {Ninguna,Salud,Viveres,Ropa,Utensilios,Crafteable};
public enum Category {Nothing,Health,Food,Clothes,Tools,Craft};
public Category category;
public enum Cloth {Nothing,Head,Glasses,Body,Legs,Shoes};
public Cloth cloth;
public int puntosQueAfecta ;
public bool sePuedeStackear;
public int maxStack;
public bool sePuedeUsar ;
public Sprite fichaitem;
public int pesoDelObjeto;
[HideInInspector]
[Range(1, 10)]
public int dificultadParaEncontrarlo=1;
[HideInInspector]public int iD = 0;
}
void Awake()
{
SetID();
}
void SetID()
{
for(int i = 0; i < objeto.Count; i++)
{
objeto[i].iD = i;
}
}
public void Update()
{
// Objeto objetoVacio=new Objeto();
// objetoVacio.nombre = "Vacio";
// objetoVacio.icono = null;
// objetoVacio.prefab = null;
// objetoVacio.iD = 0;
//
// objetoVacio.fichaitem = null;
// objetoVacio.pesoDelObjeto = 0;
//
// objetoVacio.puntosQueAfecta = 0;
// objetoVacio.sePuedeStackear = false;
// objetoVacio.sePuedeUsar = false;
// objetoVacio.maxStack = 0;
// if (objeto.Count == 0)
// objeto.Add(objetoVacio);
// else
// objeto[0] = objetoVacio;
}
}
how i convert the enum to string?
thanks again
Like with most things in C# you can simply add .ToString() to get the string.
public Cloth cloth = Cloth.Nothing;
string clothToString = cloth.ToString();
Yes i found this myself , at least something
And nowIT WORKS.I cant beleive that it was the text object abuse that mess all.I am going to clean a bit the code with your suggestions.Again, thank you very much jimroberts, i wouldn´t got this for myself for sure.I learned more with you this morning that a week in internet.
This will hopefully help you understand why your code wasn’t working. Take the following example:
public class TextObject
{
public string text = "";
}
void Awake()
{
TextObject a = new TextObject();
TextObject b = a;
//a and b now both reference the same TextObject
a.text = "Hello";
b.text = "World";
Debug.Log("a.text = " + a.text + "\r\nb.text = " + b.text);
//Output:
//a.text = World
//b.text = World
}
Yes i can see.Now, all have sense, but now, because before …