Retrieving ScriptableObject variables from list

Hello!

I’m still quite new to Unity so if this is placed in the wrong place, I hope a moderator can move this thread to the rigth place.

I’m currently having an issue with scriptableobjects that have been retrieved from a list.
What I want to do is create a scriptableobject and use that as a template for items.
Then when I want to create a new item I create the scriptableobject as an asset, change the values in the inspector and add the item to a list on a GameObject that will always be in the scene.

then at somepoint I want to generate random items with a scriptableobject that has been retrieved from the aforementioned attached to it.
I thought I could do this by storing the retrieved scriptableobject as a variable of type scriptableobject, but when I then want to retrieve the variables that are stored in the scriptableobject as bellow, it seems that I can’t acces them.

//buttonImage is a image component that has been properly linked
//inventoryIcon is the variablename that has been used in the scriptableobject script
//item is the variable I use to store the scriptableobject

buttonImage = item.inventoryIcon;

So what it comes down to is that I want to retrieve a scriptableobejct from a list that it has been added to manually and read the variables from this scriptableobject.

Whole script added bellow.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GenerateItem : MonoBehaviour
{
    public string itemName;
    public int itemType;
    public int itemNumber;
    public int amountOfWeapons;
    public int amountofConsumableWeapons;
    public int amountofConsumables;

    private int maxItems;

    ItemList itemList;
    ScriptableObject item;
    Sprite itemIcon;
    Text buttonText;
    Image buttonImage;

    void Awake()
    {
      
        itemList = GameObject.Find("SceneManager").GetComponent<ItemList>();
        buttonText = GetComponentInChildren<Text>();
        buttonImage = GetComponentInChildren<Image>();

        itemType = Random.Range(1,4);

        //check for the amount of items in the list and set the highest number to that amount.
        if (itemType == 1)
            maxItems = itemList.weapons.Count;

        if (itemType == 2)
            maxItems = itemList.consumableWeapons.Count;

        if (itemType == 3)
            maxItems = itemList.consumableItems.Count;

        //Determine what the item is going to be.
        itemNumber = Random.Range(1,maxItems + 1);
    }

    void Start()
    {
        //Set the item to the determined item.
        if (itemType == 1f)
        {
            item = itemList.weapons[itemNumber];
        }
              
        if (itemType == 2f)
        {
            item = itemList.consumableWeapons[itemNumber];
        }

        if (itemType == 3f)
        {
            item = itemList.consumableItems[itemNumber];
        }

        //Set the text of the button to the itemname
        buttonText.text = item.name;  

        //Here I want to set the imagecomponent that is a child of the button to get the image of the scriptableobject
    }
}

What’s the error/problem you get?

Is inventoryIcon a private field? In that case you have to make it public or make a property with a public getter to get the value.

All variables from the scriptableobject that I want to acces are public since this also allows me to easily edit them in the inspector instead of by editing the code.

The problem I have is that if I try it as shown in the first piece of code the item variable doesn’t give me a option to acces anything inside of it.

I’ve added an image of what options I have to add after the item variable.

3823699--322060--Knipsel.JPG

Oh I see, your variable has the wrong type:
ScriptableObject item;

You have to change ScriptableObject to the type of your item class otherwise you can only access the properties of the base class (ScriptableObject)

1 Like

Could you please elaborate?

If I change the variable type to something else I can’t acces anything from the list anymore since every item in the list is a scriptableobject with a bunch of variables stored in it.
I just need to acces the variables stored in the scriptableobjects that are stored in the list… And I can’t seem to figure out how to do that.

Do you have multiple different scriptable object classes or just one for the items?

If you have just one for the items, then can change all places where you used ScriptableObject before to your item type, for example your variable and in your item list (and all other places where you need the items)

But if you have multiple different scriptable object classes and want to store them all in the same list, then you have to use the base class ScriptableObject (as it is now) and then cast to the specific type when you need it. For example:

// ScriptableObject is the base class of all ScriptableObjects, so it can be "anything"
ScriptableObject item;
// If you want to access the variables of your item you have to cast it to your item type
var itemObject = item as YourItemType
// And then check if the ScriptableObject is really your item type
if(itemObject != null)
{
    itemObject.YourItemVariable
}
else
{
    // The variable item was either null or it's a different ScriptableObject and not your item type
}
1 Like

Thanks! That was indeed the problem.
I already had a different scriptableobject for every different type of item, so it was easy to change the things.

For anyone else who has the same problem,
I made a sperate variable for every scriptableobject of type of the scriptableobjects type. I then decided which list should be searched and then searched for the corresponding variable as which kind of type is in the list.

    Weapons weapon;
    ConsumableWeapons consumableWeapon;
    Consumable consumable;

//

        if (itemType == 1)
        {
            weapon = itemList.weapons[itemNumber];
            buttonText.text = weapon.name;
            buttonImage.sprite = weapon.inventoryIcon;
        }

Hello I am having the same problem I have a list for it because I am developing a cart system but I have multiple types of scriptable objects for ex Shop Item and Factory Item

Probably not. Please don’t necro-post to old threads.

Instead, start your own thread… it’s FREE!

When you post, consider that nobody here can read your mind OR see your screen. It is up to you to communicate effectively and completely. The following may be helpful for you in this regard:

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, especially any errors you see
  • links to documentation you used to cross-check your work (CRITICAL!!!)

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379