GetComponent.sprite is not valide. please help
Hi, I'm trying to make a game.
I have a database in Json and I'm trying to get an item from my database to my inventory but I can't get the sprite because GetComponent.sprite is not valid for me.
here is my code. please help
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class InventaireChamp : MonoBehaviour {
GameObject panelChoix;
GameObject slotChoix;
ChampDatabase database;
public GameObject choixSlot;
public GameObject choixChamp;
int nbSlot;
public List<Champ> champs = new List<Champ>();
public List<GameObject> slots = new List<GameObject>();
void Start()
{
database = GetComponent<ChampDatabase>();
nbSlot = 14;
panelChoix = GameObject.Find("Inventory");
slotChoix = panelChoix.transform.Find("SlotChoix").gameObject;
for (int i = 0; i < nbSlot; i++)
{
champs.Add(new Champ());
slots.Add(Instantiate(choixSlot));
slots[i].transform.SetParent(slotChoix.transform);
}
AddChamp(3);
}
public void AddChamp(int idCreature)
{
database = GetComponent<ChampDatabase>();
Champ champToAdd = database.FetchChampByID(idCreature);
Debug.Log(champToAdd.NomCreature);
for (int i = 0; i < champs.Count; i++)
{
if (champs[i].IdCreature == -1)
{
champs[i] = champToAdd;
GameObject champObj = Instantiate(choixChamp);
champObj.transform.SetParent(slots[i].transform);
Debug.Log(champToAdd.NomCreature);
champObj.GetComponent<Image>.sprite = champToAdd.Sprite;
champObj.transform.position = Vector2.zero;
break;
}
}
}
}
Comment
Best Answer
Answer by AntoniOpalinski · Jun 23, 2017 at 03:01 PM
ok
I forgot the ()
here is good code`using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class InventaireChamp : MonoBehaviour {
GameObject panelChoix;
GameObject slotChoix;
ChampDatabase database;
public GameObject choixSlot;
public GameObject choixChamp;
int nbSlot;
public List<Champ> champs = new List<Champ>();
public List<GameObject> slots = new List<GameObject>();
void Start()
{
database = GetComponent<ChampDatabase>();
nbSlot = 14;
panelChoix = GameObject.Find("Inventory");
slotChoix = panelChoix.transform.Find("SlotChoix").gameObject;
for (int i = 0; i < nbSlot; i++)
{
champs.Add(new Champ());
slots.Add(Instantiate(choixSlot));
slots[i].transform.SetParent(slotChoix.transform);
}
AddChamp(3);
}
public void AddChamp(int idCreature)
{
database = GetComponent<ChampDatabase>();
Champ champToAdd = database.FetchChampByID(idCreature);
Debug.Log(champToAdd.NomCreature);
for (int i = 0; i < champs.Count; i++)
{
if (champs[i].IdCreature == -1)
{
champs[i] = champToAdd;
GameObject champObj = Instantiate(choixChamp);
champObj.transform.SetParent(slots[i].transform);
Debug.Log(champToAdd.NomCreature);
champObj.GetComponent<Image>().sprite = champToAdd.Sprite;
champObj.transform.position = Vector2.zero;
break;
}
}
}
} `