Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question HELP: Acces enum type from another script

Discussion in 'Scripting' started by Slendykray, Jun 10, 2023.

  1. Slendykray

    Slendykray

    Joined:
    Mar 15, 2022
    Posts:
    2
    I have a script that store item type

    Code (CSharp):
    1. public enum itemType { type1, type2 }
    and the second script which getting the type of item you touch and doing
    appropriate stuff

    Code (CSharp):
    1.     void OnTriggerEnter(Collider collider)
    2.     {
    3.         if (collider.transform.GetComponent<items>())
    4.         {
    5.             itemType item = collider.transform.GetComponent<itemType>();
    6.          
    7.             if (item == item.type1)
    8.             {
    9.                 //do stuff
    10.             }
    11.         }
    12.     }
    but the line

    Code (CSharp):
    1. if (item == item.type1)
    is getting error:
    Member 'itemType.type1' cannot be accessed with an instance reference; qualify it with a type name instead

    How to fix it and acces the enum type?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Identifiers within other classes need to be prefaced with that class name and a dot.

    Lines 3 to 5 should be collapsed so that you only call the GetComponent<T>() once, store it to a local variable, test that variable. This is just standard DRY "Don't Repeat Yourself" programming to guard against future bugs where you change one line but not the other.

    ALSO, just for completeness:

    For internal code use, enums are great, but if you intend to serialize enums, they are bad.

    Enums enums are bad in Unity3D if you intend them to be serialized:

    https://forum.unity.com/threads/bes...if-not-do-something-else.972093/#post-6323361

    https://forum.unity.com/threads/unity-card-game-structure.1006826/#post-6529526

    It is much better to use ScriptableObjects for many enumerative uses. You can even define additional associated data with each one of them, and drag them into other parts of your game (scenes, prefabs, other ScriptableObjects) however you like. References remain rock solid even if you rename them, reorder them, reorganize them, etc. They are always connected via the meta file GUID.

    Collections / groups of ScriptableObjects can also be loaded en-masse with calls such as
    Resources.LoadAll<T>().


    Best of all, Unity already gives you a built-in filterable picker when you click on the little target dot to the right side of a field of any given type... bonus!

    ALSO: while you're making an inventory, keep this framework in mind:

    These things (inventory, shop systems, character customization, dialog tree systems, crafting, etc) are fairly tricky hairy beasts, definitely deep in advanced coding territory.

    Inventory code never lives "all by itself." All inventory code is EXTREMELY tightly bound to prefabs and/or assets used to display and present and control the inventory. Problems and solutions must consider both code and assets as well as scene / prefab setup and connectivity.

    Inventories / shop systems / character selectors all contain elements of:

    - a database of items that you may possibly possess / equip
    - a database of the items that you actually possess / equip currently
    - perhaps another database of your "storage" area at home base?
    - persistence of this information to storage between game runs
    - presentation of the inventory to the user (may have to scale and grow, overlay parts, clothing, etc)
    - interaction with items in the inventory or on the character or in the home base storage area
    - interaction with the world to get items in and out
    - dependence on asset definition (images, etc.) for presentation

    Just the design choices of such a system can have a lot of complicating confounding issues, such as:

    - can you have multiple items? Is there a limit?
    - if there is an item limit, what is it? Total count? Weight? Size? Something else?
    - are those items shown individually or do they stack?
    - are coins / gems stacked but other stuff isn't stacked?
    - do items have detailed data shown (durability, rarity, damage, etc.)?
    - can users combine items to make new items? How? Limits? Results? Messages of success/failure?
    - can users substantially modify items with other things like spells, gems, sockets, etc.?
    - does a worn-out item (shovel) become something else (like a stick) when the item wears out fully?
    - etc.

    Your best bet is probably to write down exactly what you want feature-wise. It may be useful to get very familiar with an existing game so you have an actual example of each feature in action.

    Once you have decided a baseline design, fully work through two or three different inventory tutorials on Youtube, perhaps even for the game example you have chosen above.

    Breaking down a large problem such as inventory:

    https://forum.unity.com/threads/weapon-inventory-and-how-to-script-weapons.1046236/#post-6769558

    If you want to see most of the steps involved, make a "micro inventory" in your game, something whereby the player can have (or not have) a single item, and display that item in the UI, and let the user select that item and do things with it (take, drop, use, wear, eat, sell, buy, etc.).

    Everything you learn doing that "micro inventory" of one item will apply when you have any larger more complex inventory, and it will give you a feel for what you are dealing with.

    Breaking down large problems in general:

    https://forum.unity.com/threads/opt...n-an-asteroid-belt-game.1395319/#post-8781697
     
    CodeRonnie likes this.
  3. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,362
    You need to use items.item or something if items is the component on the gameobject
    Now you are trying to getcomponent an enum, which is not possible. Only monobehaviors
     
    Bunny83 and Kurt-Dekker like this.