Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by yaezah · May 22, 2017 at 10:34 PM · uigameobjectinstantiateunity5enum

Is this the best way of using enums?

I'm trying to add categories to my items in an RPG game, so when the UI instantiates it grabs the items category (which I will be assigning to every item's prefab in the inspector) , and then it displays it in the UI under the item's name. Here's what I have so far:

 public class itemStats : MonoBehaviour {
 
     public string name;
     public enum itemType {
         CommonSword, 
         RareSword, 
         LegendarySword, 
         CommonBow,
         RareBow,
         LegendaryBow,
         CommonAxe,
         RareAxe,
         LegendaryAxe,
         CommonStaff,
         RareStaff,
         LegendaryStaff,
         ConsumableItem,
         QuestItem,
     };
     public itemType Category;
     public int Damage;
     public int Vitality;
     public int Energy;
     public string category;
 
 
     void Start () {
 
         if (Category == itemType.CommonSword) {
 
             category = "Common Sword";
         }
         else if (Category == itemType.RareSword) {
 
             category = "Rare Sword";
         }
         else if (Category == itemType.LegendarySword) {
 
             category = "Legendary Sword";
         }
 
 }
 }

And then in the UI script I have this:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 
 public class itemtypeui : MonoBehaviour {
 
     public Text stats;
     
     // Use this for initialization
     void Start () {
         stats = GetComponent<Text> ();
         //stats.color = new Color32 (255, 195, 0, 255);
         stats.text = transform.root.gameObject.GetComponent<itemStats>().category;
         //stats.fontSize = 35;
 
     }
 
     // Update is called once per frame
     void Update () {
 
     }
 }
 
 

So it essentially grabs the item's type from the script with the enum on the item prefab and then when the UI instantiates it displays the category it grabbed and prints that string. Just wanted to see if anyone had a better way of doing this. I haven't made a manager before, would this be a time to implement one? How do I assign each gameobject different value for it's variables if I added a manager instead of adding each item stats script to it ?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RobAnthem · May 22, 2017 at 11:15 PM 1
Share

I'd probably go with a Item Type and Rarity separate, like this.

 public enum ItemType
 {
     Sword,
     Axe,
     Staff
     Chestplate
 }
 public enum Rarity
 {
     Normal,
     $$anonymous$$agical,
     Rare,
     Legendary
 }


then you can do this

 public string category;
 public ItemType itemType;
 public Rarity rarity;
 void SetCategory()
 {
     category = rarity.ToString() + " " + itemType.ToString();
 }

Also, very important. Your items nor your stats should ever be $$anonymous$$onoBehaviours, it is a waste of resources. If you did this because you want to edit the data in the editor, there is much better ways, you should look into EditorWindows, and ScriptableObjects, aswell as just non-Unity class files.

All programmers should be accustomed to creating things for the sole purpose of editing data, in Unity this can be done in EditorWindows, but in normal program$$anonymous$$g we have to create a separate program to edit our data in.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

136 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

instantiate gameobjects inside a canvas 0 Answers

How to be notified of new GamObject creation? 1 Answer

UI elements is not working correctly with layouts when instantiated 1 Answer

Unwanted 2nd empty gameobject 1 Answer

2D Array of GameObjects... 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges