- Home /
passing paramters to a subclass? c#
using UnityEngine;
using System.Collections;
public class CrateController : MonoBehaviour {
public int breakStrength = 2;
public int plankLifetime = 3;
public Transform destructionLevel2;
}
using UnityEngine;
using System.Collections;
public class CrateController_DL2_Master : CrateController {
}
I'm building a destructible crate and I want to declare some parameters in the base class and pass them to its subclasses. How do I keep the variables from being exposed in the subclasses editor window?
Answer by whydoidoit · Oct 08, 2012 at 08:28 PM
Make them protected or internal rather than public.
Protected variables won't show up in the inspector for the base class though.
Ah - I see - the base class can be used on its own as well?
Right so one the derived class define the variables again as properties and get and set them with base.
public class BaseClass : $$anonymous$$onoBehaviour
{
public int somethingReallyUseful;
}
public class DerivedClass : BaseClass
{
public new int somethingReallyUseful
{
get { return base.somethingReallyUseful; }
set { base.somethingReallyUseful = value; }
}
}
That might do it.
Answer by Lukas_GMC · Oct 08, 2012 at 09:50 PM
You could write a custom editor for each object and only gui the things you need but thats probably more work than it's worth. I would just keep it with the extra things that you don't need. If you really want those things to not be editable in the base class you could keep the protected and write getter/setter and do something in them like:
if(this != BaseClass)
return;
m_var = value;
Your answer
![](https://koobas.hobune.stream/wayback/20220613082049im_/https://answers.unity.com/themes/thub/images/avi.jpg)