0

I would like to create an array that contains a GameObject and a corresponding float.

I am trying to give each enemy the ability to drop an item when they die. That item will be one of the GameObjects and the float is the probability it will be dropped.

So I created this Class:

public class ItemToDrop : MonoBehaviour {
    public GameObject itemToDrop;
    public float probability;
}

And tried using it in my Enemy class:

[SerializeField]
    private List<ItemToDrop> items;

I also tried just doing

Hastable<GameObject, float> items;

but it doesn't like being told what objects it should hold.

But that doesn't work. I tried ArrayList etc..

Is there a way to create a List, Hashtable (or other container) in this fashion?

7
  • 2
    List<ItemToDrop> should work Commented Oct 14, 2017 at 1:54
  • i have no previous experience in Unity but does unity supports list at all ?? also i am not understanding why you tried using hastable Commented Oct 14, 2017 at 4:19
  • @RJ- Unity supports List<T> just fine, I'm using them in my own project. I don't know what the problem here is though. Commented Oct 14, 2017 at 4:52
  • @Draco18s are you able to create similar file in a simple unity project ? Commented Oct 14, 2017 at 7:50
  • [System.Serializable] Have you tried using it above your "ItemToDrop" class? I use it to display custom class in editor with List<custom class> and add[SerializeField] tag to the list. Also let me understand your question you want to assign the gameobjects and float values in editor? Am i correct? Commented Oct 14, 2017 at 8:01

1 Answer 1

1

I guess, you are wanting to assign your class's variables from the inspector.
I tried out this:

[System.Serializable]
public class ItemToDrop :System.Object {
    public GameObject itemToDrop;
    public float probability;
}

And got the following output in inspector.
enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.