Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by ExtremePowers · Oct 23, 2014 at 12:39 PM · classenumif

Changing class variables

Is there a way to do this:

EDIT: I changed the code below

 enum ItemType { 
     Resource = 0, 
     Weapon = 1,
     Medical = 2,
     Building = 3
 }
 
 enum MedicalType {
     Heal = 0,
     Feed = 1,
     Drink = 2,
     Multiple = 3
 }
 
 class item {
     var Name : String;
      var Type : ItemType = ItemType.Resource;
     var texture : Texture2D;
     if (Type == ItemType.Building) {    
         var Building : GameObject;
     }
     if (Type == ItemType.Medical) {
         var MedType : MedicalType = MedicalType.Heal;
         var MedicalAmount : int;
     }
     if (Type == ItemType.Weapon) {
         var Weapon : GameObject;
         var AmmoType : String;
         var Range : float;
     }
 }
Comment
Add comment · Show 2
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 fafase · Oct 23, 2014 at 01:58 PM 0
Share

Could you describe what you are after because you seem to have it already. Just place the if in a Start and done with it.

avatar image richyrich · Oct 23, 2014 at 03:12 PM 0
Share
 using UnityEngine;
 using System.Collections;
 
 public class Item : $$anonymous$$onoBehaviour
 {
     public string name = "";
 }
 
 public class Weapon : Item
 {
     public int count = 0;
 }
 
 public class Resource : Item
 {
     public Texture2D texture;
 }
 
 public class $$anonymous$$ainClass : $$anonymous$$onoBehaviour {
     public GameObject[] gameObjects = new GameObject[4];
     // Use this for initialization
     void Start()
     {
         //Replace Cube_P0 with existing GO etc
         gameObjects[0] = GameObject.Find("Cube_P0");
         gameObjects[0].AddComponent<Weapon>();
     }
 
     Item item = new Weapon();
 }
 

By inheriting from $$anonymous$$onBehaviour, you can add the class as a component.

Alternatively, add Item as a variable e.g.

Item item; item = new Weapon(); item = new Resource();

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by richyrich · Oct 23, 2014 at 12:49 PM

Yes, but the code would be meaningless given both integers within the enum are currently set to the same value. Also, you can't declare a variable within the if-statement and then not use it.

Comment
Add comment · Show 8 · Share
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 ExtremePowers · Oct 23, 2014 at 12:54 PM 0
Share

What do you mean I can't declare a variable in the if-statement and then not use it? I want there to be some options specified by which type the Type is.

avatar image richyrich · Oct 23, 2014 at 01:42 PM 0
Share

If you declare a variable inside an if-statement, then it will 'fall out of scope' when the if-statement ends. If you want the value to persist outside the if-statement, then you need to declare the variable outside and then modify it within the if-statement. Alternatively, have a data structure (declared outside if-statement) like an array or List that you set/add to inside the if-statement.

avatar image ExtremePowers · Oct 23, 2014 at 01:49 PM 0
Share

I have made an public array and wanted to make the items manually in the editor, but I wouldn't want to show the variables in the editor which had no use. So I want to be able to have some variables specified by which type it has.

avatar image Itaros · Oct 23, 2014 at 01:58 PM 0
Share

Then writing a custom inspector for it is suitable tactic and nulling unneeded variables

avatar image richyrich · Oct 23, 2014 at 02:03 PM 0
Share

Let me see if I understand you correctly...

You want to have a system whereby you have a collection of objects, but you don't know (before the game begins running) exactly what type these objects are, only that they are all 'Items'. If it is a 'Resource' Item, then it has a Texture2D, but if it is a 'Weapon' then it has an int. Is this correct?

Show more comments
avatar image
0

Answer by Itaros · Oct 23, 2014 at 12:52 PM

Use C# and polymorphism:

 public abstract class item{
 public string name;
 }
 
 public class resource:item{
 public Texture2D texture;
 }
 
 public class weapon:item{
 public int count;
 }
Comment
Add comment · Share
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

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

29 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

Related Questions

Using an enum in a class constructor. 1 Answer

Enum style variable 2 Answers

Finding the length of an enum in a different class 1 Answer

Unknown identifier: 'types'. js(38,61) 1 Answer

Logic and datatype existential problem 2 Answers


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