- Home /
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,
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.
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
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?
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!
Your answer
![](https://koobas.hobune.stream/wayback/20220613102705im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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