How to spawn 3d Objects in Display View

Shade

New member
I am trying to spawn 3d objects when a "Pet" Icon is selected in the inventory, and display it like Player in the demo (Raw Character Image). I tried created my own Item View Attribute, and attempted to modify your code for spawning a pet after buying one at a shop, but I am getting a Null Error on the petPrefab. The Prefab is on the item, and the attribute name is correct. Ultimately I want it to look like a character select. Any help would be appreciated.
Code:
public override void SetValue(ItemInfo info)
        {
            var attribute = info.Item.GetAttribute(m_AttributeName);
            if (attribute == null) { return; }
            var petPrefab = info.Item.GetAttribute<Attribute<GameObject>>("PetPrefab").GetValue();           
            var instance = Instantiate(petPrefab);
            
        }
 
Why are you first checking for m_AttributeName and then for "PetPrefab"?

Also if you aren't going to change the attribute value I recommend using the try get attribute value function instead:

Code:
if (info.Item == null) {
    Debug.LogWarning("Item is null");
    return;
}

if (info.Item.TryGetAttributeValue(m_AttributeName, out GameObject prefab) == false) {
    Debug.LogWarning($"Prefab attribute value not found for attribute name {m_AttributeName}");
    return;
}

if (prefab == null) {
    Debug.LogWarning("Attribute exists, but no prefab was assigned.");
    return;
}

Instantiate(prefab);
 
Back
Top