Hiding values???

Hello,
What if I developed a class that had serialized values, like

Class class1 {
   public float value1;
   public float value2;
   public float value3;
}

public class1 class1Variable = new class1();

and I later embedded class1 inside class2, like

Class class2 {
   public class1 c1;
   public float value1;
   public float value2;
   public float value3;
}

public class2 class2Variable = new class2();

but I come to LATER find that I don’t want class2Variable.c1 to have value3 as a property? Another words, I want class1Variable.value3 to exist, but class2Variable.c1.value3 to be obsolete. How would I do this? Thank you.

Short answer is you can’t. Classes have the same number of fields regardless of where you use them.

You could accomplish what you’re describing with inheritance, but the fact that you mentioned serializable at the top suggests to me you’re encountering a problem related to serialization.

Describe further what it is you’re trying to accomplish.

The reason I mentioned serialization is because I wanted to see everything in the inspector. What I provided at the top was only an example of what I believe can be a common problem for most programmers when they later start realizing that there are some unwanted fields that show up in other superclasses that they didn’t intend to embed, and would like to go back and fix the problem without having to rewrite a whole bunch of code after fixing the fields in their subclasses. What I was hoping to accomplish would be something like de-heritance.

I don’t agree that what you are describing is a common problem.

A proper grasp on unity, serialization and inheritance will allow you to write code that shows up in the inspector as you intend.

If the class you are looking at in the inspector has unwanted fields, use a different class.

1 Like

You can also write a custom inspector. Works well for things like having a check box that turns stuff on and off.

But I tend to agree with @BenZed , it sounds like you have an underlying structural problem.

You can use the NonSerialized attribute to hide fields in the inspector.

1 Like

Or [HideInInspector] if you still want it serialized.

2 Likes