- Home /
Use a enum from another script
hi, i want to use a enum from another script, but i dont know how.. i'm making an inventory, and i want to when i drag and drop a item to verify if the item that i just drop is from the same type of the item that is already in the slot, and if it is to change the sprite. this is my code (i'm just going to put here the part of the drop the item) this is the parte of the script from inventory: Class Inventory:
//drop the item in other slot
if (e.type == EventType.mouseUp && draggingItem) {
//verefy if the object is the same type of the other
if (draggingItem == Item.ItemType.OtherObjects) {
//change the sprite
}
}
else {
inventory [prevIndex] = inventory [i];
inventory [i] = draggedItem;
draggingItem = false;
draggedItem = null;
}
and this is from the Item, where i have the enum: Class Item:
public enum ItemType
{
DoorKeys,
Papers,
OtherObjects,
chaveParte
}
Answer by pako · Jan 06, 2018 at 05:39 PM
For ItemType
to be accessible independently of the Item
class, (e.g. ItemType.DoorKeys
) put it's definition in the same script as Item.cs but after the closing brackets of the Item class:
//Item.cs
public class Item{
//your code
}
public enum ItemType
{
DoorKeys,
Papers,
OtherObjects,
chaveParte
}
For ItemType
to be accessible as part of the Item
class, (e.g. Item.ItemType.DoorKeys
) put it's definition in the same script as Item.cs but inside the closing brackets of the Item class:
//Item.cs
public class Item{
public enum ItemType
{
DoorKeys,
Papers,
OtherObjects,
chaveParte
}
//your code
}
Well, usually that's how you do it...
However, if you want the enum in its own script, just create a new script, name it "ItemType" and then open it for editing (VS/$$anonymous$$ono).
When it's open you'll have:
public class ItemType : $$anonymous$$onoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
Just change "class" to "enum", and delete "$$anonymous$$onoBehaviour", and the Start() and Update() methods, and change to:
public enum ItemType {
Door$$anonymous$$eys,
Papers,
OtherObjects,
chaveParte
}
Answer by EmmetOT · Jan 07, 2018 at 04:59 PM
Assuming you have an "Item" class which contains the public enum "ItemType", you can call it from other classes by using "Item.ItemType."
Or you can just declare the enum outside of any class.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How can i change the camera view when clicking a button ? 1 Answer
Can I add an enum value in the inspector? 4 Answers
How can i limit object dragging area in my inventory? 0 Answers