- Home /
Getting a reference to a group of ScriptableObjects implemented at runtime?
So I think that title's appropriate, but here's what's going on:
I have about 30 scriptable objects, of type GameItem:
public class GameItem : ScriptableObject {
public int itemID;
public string itemName;
public string merchantName;
public string itemDesc;
public string cost;
public Sprite merchantIcon;
}
And what I can currently do with this is select 8 ScriptableObjects at random (eight items), from a list of all of them, and then put those 8 in a scroll-down display panel.
What I want to do next is to open up a new window whenever I select a button, displaying whatever item advertised by said button.
So here's the selection process for my eight buttons:
foreach (var item in itemsList)
{
if(randomValues.Contains(item.itemID))
{
GenerateInfo(item);
(randomValues is an array which stores eight random numbers within the constraints of itemList.Count).
This line:
GenerateInfo(item);
Is what I want to talk about, because I'm passing the item through into another method 'GenerateInfo', which I want to deal with opening up the other window to display further information on the item.
I have an:
public Button.ButtonClickedEvent openInfo;
Which calls GenerateInfo.
In the above image you can see that currently there isn't a reference to a variable of type GameItem.
In this case, it is referring to:
public void GenerateInfo(GameItem anItem)
anItem, which I use to set the FurtherInformation panel's various properties to equal the properties of that variable. Say I want to display the title of whatever item is pressed, my logic is to take the current item and then access it's 'name' property.
THE PROBLEM
I don't know what to put down as a reference for this variable of type GameItem. I have the option to put down one of my scriptable objects, but that would defeat the point of selecting eight at random from my list. Since what object is used is subject to the if statement, comparing the item's ID to a random number generated.*
Let me know if there's anything I need to clear up, I have considered making the variable private.
It's not really clear to me what the problem is...
You have a reference to a GameItem to do something with. So what GameItem do you want to use there? Then put that one in the reference... That's probably not much help but I don't really understand your question.
@siaran I don't know what to put into the slot, since the GameItem used is subject to random selection. To explain, I loop through a list of all scriptable objects of type GameItem, I select eight at RANDO$$anonymous$$ using an array. For each item selected as I loop through I send that item to the method GenerateInfo, which takes it as a parameter. In GenerateInfo I use that GameItem to populate the information (UI Text, Images etc.) on a seperate panel. That's the exercise. But you see, through all that I never actually $$anonymous$$NOW definitively which GameItem will be selected so I CANT put down a reference in the editor, because the process is random.
So you can just assign it in your script after you've made a selection then, right?