I have an inventory in the game and the selection of objects is implemented, respectively, with the transfer to the inventory.
Now I need to randomize the loss of objects under the flaunt, conditionally, boxes.
Can’t figure it out how to contact a certain subject? Now the appeal works like this:
item [i] = hit.collider.getcomponent & lt; item & gt; (); // that is, the subject i = the subject I was crossed
Inventory code:
list & lt; item & gt; Item;
Public GameObject CellContainer;
Public Keycode Showinventory;
Public Keycode Takebutton;
Void Start ()
{
Item = New List & lt; Item & gt; ();
CellContainer.SetActive (False);
For (int i = 0; i & lt; cellcontainer.transform.childcount; I ++)
{
Item.Add (new item ());
}
}
// Update IS Called Once Per Frame
Void Update ()
{
ToggleInventory ();
AddToInventory ();
}
Void AddToInventory ()
{
If (input.getKeydown (Takebutton))
{
Debug.log ("Takebutton");
Ray Ray = Camera.Main.ScreenPointToAY (new vector2 (screen.width / 2, screen.height / 2));
Raycasthit Hit;
if (pysics.raycast (Ray, Out Hit, 1000F))
{
Debug.log ("Raycast");
if (hit.collider.getcomponent & lt; item & gt; ())
{
Debug.log ("BEFORE THE FOR");
for (int i = 0; i & lt; item.count; i ++)
{
if (Item [i] .id == 0)
{
Item [i] = hit.collider.getcomponent & lt; item & gt; ();
DisplayItems ();
Break;
}
}
}
}
}
}
Void ToggleInventory ()
{
if (input.getKeydown (showinventory))
{
If (CellContainer.ActiveVeself)
{
CellContainer.SetActive (False);
}
ELSE.
{
CellContainer.SetActive (True);
}
}
}
Public Void DisplayItems ()
{
for (int i = 0; i & lt; item.count; i ++)
{
TRANSFORM CELL = CellContainer.Transform.Getchild (I);
TRANSFORM icon = cell.getchild (0);
Image img = icon.getcomponent & lt; image & gt; ();
If (Item [i] .id! = 0)
{
img.Enabled = True;
img.Sprite = Resources.Load & LT; Sprite & GT; (Item [i] .pathicon);
}
ELSE.
{
img.Enabled = false;
img.Sprite = NULL;
}
}
}
}
Item code:
public string nameitem;
Public int id;
Public int AmountItem;
Public bool isstackable;
[Multiline (5)]
Public String DescriptionItem;
Public String Pathicon;
Public String PathPrefab;
item I apply to a new object on the map. I tried to score LIST objects from hand and use them, but it did not work out, the items were not displayed, somewhere closed null’y, and so
07/01/2020 Okay, I tried today to create a list of items manually and randomly withdraw one of these items in the inventory, did this:
int rand = random.range (1, 5);
for (int i = 0; i & lt; item.count; i ++)
{
if (Item [i] .id == 0)
{
Item [i] = item [Rand];
DisplayItems ();
Break;
}
}
Error occurs in DisplayItems, writes that “TRANSFORM CHILD OUT OF BOUNDS” in the line
“transform cell = celcontainer.transform.getchild (i);
” (DisplayItems).
scored the list so:
item = new list & lt; item & gt; () {
New Item {id = 3, nameItem = "Pear", Pathicon = "icon / hleb", pathprefab = "prefabs / apple"},
New Item {ID = 4, NameItem = "Cherry", Pathicon = "icon / apple", PathPrefab = "Prefabs / Apple"},
new item {id = 5, nameItem = "Mandarin", Pathicon = "icon / hleb", pathprefab = "prefabs / apple"}
};
On the stage, respectively, there are no such objects, but is only in the list.
Again, there is a question, as I contact the object with the Item script, there is no it in the list, and there is a scene.
Now everything works like this:
I create an object on the stage, throwing the Item script on it, he, accordingly, becomes the subject, I can pick it up when you press the button when I look at him, but it does not suit me.
I read a little now about this error, and it seems to me that DisplayItems sees only those Items that are on stage ….
Answer 1, Authority 100%
everything, figured out. Created a database of all objects that is – (made the platform and put all items on it) – after which it pulls all the data from there, and now everything works as needed. Thank you all for your attention!