custom Mystery Box - displaying weapons problem

im making a free mystery box with the idea that it is easy to use without having to edit alot of code.
so i made this in the inspector:

all you would have to do is setting the values.
now i want to instantiate each weapon from the item array based on a random number (0 - array lenght)
raising it out of the mystery box after the animation (opening the box) is done. and clearing it when its not picked up.
how would i do that?

what i have now is this:

 public MysteryBoxItems[] items;

void Update()
{
        for (int i = 0; i < items.Length; i++)
        {
            MysteryBoxItems insertedItems = items[i];
            Debug.Log("found item with the name: " + insertedItems.itemName);
        }
}

and the mysteryBoxItems script contains this:

using UnityEngine;

[System.Serializable]
public class MysteryBoxItems{
    [Header("item info")]
    public string itemName;
    public GameObject Item;
    [Space]
    [Header("ammo")]
    public int ammoPerRound;
    public int maxRounds;
    [Space]
    [Header("display")]
    public Vector3 rotation;
    public Vector3 scale;
}

can someone help me?
(the animations and all work. just need help with the instantiating the weapons correctly with the given values)

Thanks alot!
kelvin

You don’t need to for-loop through the items if you’re just picking one of them at random.

int i = Random.Range(0, items.Length);
MysteryBoxItems mbItem = items[i];

Instantiating the weapon should be pretty straightforward, but

takes an extra step. It will depend on how your spawned prefab is set up. Assuming you have a script that will be attached to any prefab you’d spawn here:

GameObject spawnedItemGO = Instantiate(mbItem.item);
SomeItemClass inWorldItem = spawnedItemGO.GetComponent<SomeItemClass>();
inWorldItem.ammoPerRound = ammoPerRound;
//etc
1 Like

k thnx so i got it to work but it is now a very long code. any tips to make it smaller?

void Update()
{
        if (moving && instantiateRandomItem != null)
        {
            Vector3 nextPos = new Vector3(instantiateRandomItem.transform.position.x, transform.position.y + 1.25f, instantiateRandomItem.transform.position.z);
            if (instantiateRandomItem.transform.position != nextPos)
            {
                instantiateRandomItem.transform.position = Vector3.MoveTowards(instantiateRandomItem.transform.position, nextPos, 0.5f * Time.deltaTime);
            }
        }else
        {
            moving = false;
        }
    }

    void randomItem()
    {
        i = Random.Range(0, items.Length);
        Debug.Log("found item with the name: " + i.ToString());
        MysteryBoxItems insertedItems = items[i];
        Vector3 position = spawnItem.position;
        Vector3 r = insertedItems.rotation;
        Quaternion rotation = Quaternion.Euler(r);
        Vector3 scale = insertedItems.scale;
        instantiateRandomItem = Instantiate(insertedItems.Item, position, rotation) as GameObject;
        instantiateRandomItem.name = insertedItems.itemName;
        instantiateRandomItem.transform.localScale = insertedItems.scale;
        instantiateRandomItem.tag = "PickUpItem";
        moving = true;
    }

Well, you’ve got some unnecessary local variables in there (lines 21-24 are unnecessary; those values can be inserted directly into the function calls without needing to assign them to a local variable first). Other than that, not really.

k thanks alot!!