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 MadoScien · Apr 13, 2015 at 01:07 PM · variableinspectorruntimedynamic

Adding variables to a class at runtime

Hey guys, i'am making block editor for my game, the script is working in edit mode. I'am having a problem with adding variables for block creators when i choose a block type.

alt text

For example, when i choose "Gun" option the script should add new variables to class like "ShootingSpeed", and later the editor script should show it in the inspector. The problem lies in adding these variables, it seems that i'cant modify classes dynamicly or just add a new variables to class at runtime in C#.

 [ExecuteInEditMode]
 public class BlockCreator : MonoBehaviour
 {
 
     public enum Type // <---- This is a type enum, used for defining the type of the block and extending it's functionality
     {
         Hull,
         Gun,
         Engine,
         Utility,
             
     };
 
 
     public enum Collidable
     {
         Yes,
         No,
     };
         
     public int BX; //Volume variables
     public int BY;//Volume variables
     public float mass;
     public int HP;//Hit points
     public int Armor;
     public Type BlockType;
     public Collidable IsBlockCollidable;
     
     void Update()
     {
     
         if (BlockType = Type.Gun) {
                 // Add Variables or do other things to add variables 
         }
 
     }
     
 
     public void CreateBlock()
     {
     //TODO generate a new prefab made with variables from BlockCreator Class
     }
 }


 

And the BlockCreatorEditor

 [CustomEditor(typeof(BlockCreator))]
     public class BlockCreatorEditor : Editor
     {
 
          public override void OnInspectorGUI ()
         {
             DrawDefaultInspector ();
 
             BlockCreator myBlockCreator = (BlockCreator)target;
             
         if(GUILayout.Button("Create Block"))
             {
             myBlockCreator.CreateBlock();
             }
 
         }
     
     }
 

So in the end: Is there any way to add dynamicly variables at run time, and possibly how to do it? It would be the best to make it in C# but JavaScript can be used too.

przechwytywanie.png (12.3 kB)
Comment
Add comment · Show 3
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 meat5000 ♦ · Apr 12, 2015 at 01:52 PM 0
Share

Use structs ins$$anonymous$$d of enums perhaps.

avatar image Glurth · Apr 13, 2015 at 01:35 PM 3
Share

I don't know of anyway to add a variable to a class at runtime. I would probably have made GUN a CO$$anonymous$$PONENT/mono-class with it's own variables defined at compile-time: you can always add/remove a component to a game object, when the block type is changed.

avatar image hexagonius · Apr 13, 2015 at 03:59 PM 0
Share

Only thing I've ever heard of but never used (sounds fancy though) is this:

https://msdn.microsoft.com/de-de/library/system.reflection.emit.typebuilder%28v=vs.110%29.aspx

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by MadoScien · Apr 14, 2015 at 04:42 PM

Thanks everybody for helpful comments. I've used Glurth method but slightly changed it. What i have done is that i have the types already written in program, so i'am "linking" them to the "block editor" and than oparates on whole object already. The editor is used only for linking and creating the prefab from the stuff that i wrote earlier.

Again thanks everybody for help!

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

6 People are following this question.

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

Related Questions

how to link variable as inspector field 1 Answer

Custom Inspector - How to add functionality? 1 Answer

Parent Class variables in Child Class's Inspector (C#) 0 Answers

Instantiate a prefab vs create one dynamically at runtime? 1 Answer

Variable data type for two ints (x and y) in inspector 3 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