- Home /
Adding items to inventory with random values
I have created an inventory system that is still in its early stages. At the moment the issue iI am having is with the Item's properties when added to the inventory. The items have a member variable that is randomized in the start function, using the inspector I have confirmed that the values are being randomized but when they are added to the inventory all items have the same randomized value.
example
name: gold
weight : 01
purity : xx (random range from 1 to n)
If I create 5 pices of gold they all show different purity in the inspector but when added to the inventory they all show the same number.
Without seeing a snip of at least a small bit of code it could be just about anything..
Answer by tw1st3d · Oct 03, 2013 at 07:07 PM
This is somewhere along the lines of what I'd do. This is quite complex for something that "seems" small, but trust me this is the system you'll want for a vast inventory system.
using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public interface Item
{
enum Purity{
POOR = 1,
COMMON = 2,
FINE = 3,
PRISTINE = 4
}
public int Weight(int weight);
public Item makeItem(Item.Gettable.Items itemName, int weight, Item.Purity purity);
// Return a new Item
}
public interface Gettable : Item
{
enum Items{
GOLD = 1,
COPPER = 2
}
}
public class AddItem : MonoBehavior, Gettable
{
public Item newItem;
public void Update()
{
Items theItem = (Items) Random.Range(1,2);
Purity purity = (Purity) Random.Range(1,4);
newItem = makeItem(theItem, 1, purity);
}
public Item makeItem(Item.Gettable.Items itemName, int weight, Item.Purity purity)
{
Item theItem;
theItem.Items = itemName;
theItem.Weight = weight;
theItem.purity = purity;
}
}