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 Bunnybomb7670 · May 21, 2013 at 09:22 PM · classclassesaccessvoxelblock

Storing a Class in a variable

Ive been having issues with this for quite a bit now and cant find anything on the wiki / answers about this . Basically, I have a Block class for my voxel engine, each voxel has an individual instance of the Block class, this class basically stores the main aspects of the block, the ID , the texture data and stuff, but I wanted to implement a feature that allowed me to have custom classes for each block so that I can make them store custom variables and also make them have features individually.

I am having trouble thinking of a way to fill an array of 32x32x32 values with different block classes, I thought of having all the classes as blocks and then having a variable hold the secondary " feature " class of the block , this is how I do it so far :

 // I was not sure how to store it so I did this :
 
 public object customClass;
 
 // I then had a switch to manage which class to instance 
 
 switch(blocktype){
 
             case 0:
             customClass= new BlockEngine(this,1.0f);
             break;

I just dont know how I would access the class from another script because I cannot do :

Ship.GetComponent().voxels[0,0,0].customClass.whatever();

because it does not give me the option to access it through that. If anybody could show me a way to store a kind of changeable class type variable, I would be very grateful!

Thanks,

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 Bunnybomb7670 · May 21, 2013 at 09:25 PM 0
Share

Thing is, i want to create an instance of BlockEngine or whatever class is in the case and then store it, if you know what I mean.

avatar image roojerry · May 21, 2013 at 09:29 PM 0
Share

Sorry I seem to have misunderstood your question. What are these secondary feature classes? To me it sounds like a good place to use inheritance

avatar image Bunnybomb7670 · May 21, 2013 at 09:32 PM 0
Share

So far I have BlockEngine, but ill be adding more custom classes. should I be inheriting from the block class and making a new Block, or something like that, I never went into the realms of inheritance because I never needed it. I guess I should be making a class called BlockFeature and then make different classes that inherit from BlockFeature and then store customClass as a BlockFeature?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunnybomb7670 · May 21, 2013 at 10:37 PM

Ok so, I think I actually figured a way around this, however it is not the most efficient way. I did this :

 public BlockFeature customClass;

then I initialized it using a case system , like before :

     case 0:
             customClass= new BlockEng();
             break;

Then I had a " somewhat clever " way around the fact that derived classes work through their base class, so instead, I did this :

 public class BlockFeature {
     public virtual void doStuff(string a)
     {
     }
 }

Made a basic class with a virtual void function, which was overridden by the other class :

 class BlockEng : BlockFeature{
     public override void doStuff(string a){
     switch(a){
         case "getThrust":
         getThrust();
         break;
         }
     }
     public void getThrust () {
         Debug.Log ("THRUST!");
     }
 }
 

which allows me to call functions in this derived class through the main class allowing me to technically store these classes in a single variable that is different for every block!

If anybody has a more efficient way, please, tell me because this way is in no way efficient, it just works for now!

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

14 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

Related Questions

Accessing classes class from other script 2 Answers

How would I access a Class Variable from another script? 1 Answer

Access a class on another script? 1 Answer

Need my function to work with different lists of different values (classes) 1 Answer

HELP! Block placing and destroying script doesn't work properly. 0 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