Search Unity

custom Mystery Box - displaying weapons problem

Discussion in 'Scripting' started by kelvin-w, Mar 31, 2017.

  1. kelvin-w

    kelvin-w

    Joined:
    Nov 25, 2016
    Posts:
    74
    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:
    Code (CSharp):
    1.  
    2.  public MysteryBoxItems[] items;
    3.  
    4. void Update()
    5. {
    6.         for (int i = 0; i < items.Length; i++)
    7.         {
    8.             MysteryBoxItems insertedItems = items[i];
    9.             Debug.Log("found item with the name: " + insertedItems.itemName);
    10.         }
    11. }
    and the mysteryBoxItems script contains this:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [System.Serializable]
    4. public class MysteryBoxItems{
    5.     [Header("item info")]
    6.     public string itemName;
    7.     public GameObject Item;
    8.     [Space]
    9.     [Header("ammo")]
    10.     public int ammoPerRound;
    11.     public int maxRounds;
    12.     [Space]
    13.     [Header("display")]
    14.     public Vector3 rotation;
    15.     public Vector3 scale;
    16. }
    17.  
    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
     
    Last edited: Mar 31, 2017
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    You don't need to for-loop through the items if you're just picking one of them at random.
    Code (csharp):
    1. int i = Random.Range(0, items.Length);
    2. 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:
    Code (csharp):
    1. GameObject spawnedItemGO = Instantiate(mbItem.item);
    2. SomeItemClass inWorldItem = spawnedItemGO.GetComponent<SomeItemClass>();
    3. inWorldItem.ammoPerRound = ammoPerRound;
    4. //etc
     
    kelvin-w likes this.
  3. kelvin-w

    kelvin-w

    Joined:
    Nov 25, 2016
    Posts:
    74
    k thnx so i got it to work but it is now a very long code. any tips to make it smaller?

    Code (CSharp):
    1. void Update()
    2. {
    3.         if (moving && instantiateRandomItem != null)
    4.         {
    5.             Vector3 nextPos = new Vector3(instantiateRandomItem.transform.position.x, transform.position.y + 1.25f, instantiateRandomItem.transform.position.z);
    6.             if (instantiateRandomItem.transform.position != nextPos)
    7.             {
    8.                 instantiateRandomItem.transform.position = Vector3.MoveTowards(instantiateRandomItem.transform.position, nextPos, 0.5f * Time.deltaTime);
    9.             }
    10.         }else
    11.         {
    12.             moving = false;
    13.         }
    14.     }
    15.  
    16.     void randomItem()
    17.     {
    18.         i = Random.Range(0, items.Length);
    19.         Debug.Log("found item with the name: " + i.ToString());
    20.         MysteryBoxItems insertedItems = items[i];
    21.         Vector3 position = spawnItem.position;
    22.         Vector3 r = insertedItems.rotation;
    23.         Quaternion rotation = Quaternion.Euler(r);
    24.         Vector3 scale = insertedItems.scale;
    25.         instantiateRandomItem = Instantiate(insertedItems.Item, position, rotation) as GameObject;
    26.         instantiateRandomItem.name = insertedItems.itemName;
    27.         instantiateRandomItem.transform.localScale = insertedItems.scale;
    28.         instantiateRandomItem.tag = "PickUpItem";
    29.         moving = true;
    30.     }
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    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.
     
  5. kelvin-w

    kelvin-w

    Joined:
    Nov 25, 2016
    Posts:
    74
    k thanks alot!!