- Home /
Benefit of nested classes?
What's the benefit of using nested classes? For example, I have a "PlayerObjects" script which caches the wheel Transforms, WheelColliders and frame BoxCollider of the Bike gameObject.
At first my script was setup as followed:
public class PlayerObject : MonoBehaviour {
// Cache player gameObject
private GameObject playerGO;
public GameObject Player
{
get {
if (playerGO == null)
{
Debug.LogError("There is no player gameObject in the scene. Make sure the player gameObject is tagged 'Bike'!");
}
return playerGO;
}
}
// Cache "Colliders" gameObject
private Transform bikeCollidersParent;
public Transform BikeColParent
{
get {
return this.bikeCollidersParent;
}
set {
bikeCollidersParent = value;
}
}
// Reference WheelColliders and Wheel Transforms (cache on Awake function)...
private WheelCollider frontWheelCollider;
public WheelCollider FrontWC {
get {
return this.frontWheelCollider;
}
set {
frontWheelCollider = value;
}
}
private Transform frontWheelTransform;
public Transform FrontWT {
get {
return this.frontWheelTransform;
}
set {
frontWheelTransform = value;
}
}
private WheelCollider rearWheelCollider;
public WheelCollider RearWC {
get {
return this.rearWheelCollider;
}
set {
rearWheelCollider = value;
}
}
private Transform rearWheelTransform;
public Transform RearWT
{
get {
return this.rearWheelTransform;
}
set {
rearWheelTransform = value;
}
}
// ******************************* AWAKE FUNCTION ******************************* //
void Awake () {
// Cache player gameObject
playerGO = GameObject.FindWithTag("Bike");
// Check if the player's transform components exist and cache them if they do
foreach (Transform t in playerGO.GetComponentsInChildren<Transform>())
{
if (t.name == "Colliders")
bikeCollidersParent = t;
if (t.name == "Front Wheel")
{
frontWheelTransform = t;
}
if (t.name == "Rear Wheel")
{
rearWheelTransform = t;
}
}
// Cache player's wheelcollider components
foreach (WheelCollider wc in playerGO.GetComponentsInChildren<WheelCollider>())
{
if (wc.name == "frontWheelCollider")
{
frontWheelCollider = wc;
}
if (wc.name == "rearWheelCollider")
{
rearWheelCollider = wc;
}
}
// If the bikeCollidersParent doesn't exist, create it...
if (bikeCollidersParent == null)
{
bikeCollidersParent = new GameObject("BikeColliders").transform;
bikeCollidersParent.parent = playerGO.transform;
if (bikeCollidersParent == null)
print("No Colliders Found in player object");
}
// If the WheelColliders don't exist, then create them...
if (frontWheelCollider == null)
{
frontWheelCollider = new GameObject("Front WheelCollider").AddComponent<WheelCollider>();
frontWheelCollider.transform.parent = bikeCollidersParent;
frontWheelCollider.transform.position = frontWheelTransform.position;
frontWheelCollider.transform.rotation = frontWheelTransform.rotation;
frontWheelCollider.radius = frontWheelTransform.renderer.bounds.size.y / 2;
}
if (rearWheelCollider == null)
{
rearWheelCollider = new GameObject("rearWheelCollider").AddComponent<WheelCollider>();
rearWheelCollider.transform.parent = bikeCollidersParent;
rearWheelCollider.transform.position = rearWheelTransform.position;
rearWheelCollider.transform.rotation = rearWheelTransform.rotation;
rearWheelCollider.radius = rearWheelTransform.renderer.bounds.size.y / 2;
}
} // Awake function end
Then I thought of using a nested class to store the wheel Transforms and WheelColliders, as bellow:
public class PlayerObject : MonoBehaviour {
// Cache player gameObject
private GameObject playerGO;
public GameObject Player
{
get {
if (playerGO == null)
{
Debug.LogError("There is no player gameObject in the scene. Make sure the player gameObject is tagged 'Bike'!");
}
return playerGO;
}
}
// Cache "Colliders" gameObject
private Transform bikeCollidersParent;
public Transform BikeColParent
{
get {
return this.bikeCollidersParent;
}
set {
bikeCollidersParent = value;
}
}
// Define WheelCollider settings in case they need to be created at runtime
public float wheelRadius = 0.3f;
public float damperForce;
public float wheelMass;
public float springForceFront;
public float springForceRear;
public float suspensionDistance;
public float targetPosition;
// Create the wheel class
private class Wheel
{
public WheelCollider wCollider;
public Transform wGraphic;
public Quaternion originalRotation;
// Constructor
public Wheel()
{
}
}
private Wheel[] wheels = new Wheel[2];
public WheelCollider frontWC
{
get {
return wheels[0].wCollider;
}
}
public WheelCollider rearWC
{
get {
return wheels[1].wCollider;
}
}
public Transform frontWT
{
get {
return wheels[0].wGraphic;
}
}
public Transform rearWT
{
get {
return wheels[1].wGraphic;
}
}
public Quaternion OriginalRotation
{
get {
return wheels[0].originalRotation;
}
}
// ******************************* AWAKE FUNCTION ******************************* //
void Awake () {
// Cache player gameObject
playerGO = GameObject.FindWithTag("Bike");
wheels[0] = new PlayerObject.Wheel();
wheels[1] = new PlayerObject.Wheel();
// Check if the player's transform components exist and cache them if they do
foreach (Transform t in playerGO.GetComponentsInChildren<Transform>())
{
if (t.name == "Colliders")
bikeCollidersParent = t;
if (t.name == "Front Wheel")
{
wheels[0].wGraphic = t;
}
if (t.name == "Rear Wheel")
{
wheels[1].wGraphic = t;
}
}
// Cache player's wheelcollider components
foreach (WheelCollider wc in playerGO.GetComponentsInChildren<WheelCollider>())
{
if (wc.name == "frontWheelCollider")
{
wheels[0].wCollider = wc;
}
if (wc.name == "rearWheelCollider")
{
wheels[1].wCollider = wc;
}
}
// If the bikeCollidersParent doesn't exist, create it...
if (bikeCollidersParent == null)
{
bikeCollidersParent = new GameObject("BikeColliders").transform;
bikeCollidersParent.parent = playerGO.transform;
if (bikeCollidersParent == null)
print("No Colliders Found in player object");
}
if (wheels == null)
{
foreach (Wheel w in wheels)
{
w.originalRotation = w.wGraphic.rotation;
// Create collider
GameObject colliderObj = new GameObject(w.wGraphic.name + "Collider");
colliderObj.transform.parent = bikeCollidersParent;
colliderObj.transform.position = w.wGraphic.position;
w.wCollider = colliderObj.AddComponent<WheelCollider>();
w.wCollider.suspensionDistance = suspensionDistance;
w.wCollider.mass = wheelMass;
w.wCollider.radius = wheelRadius;
JointSpring js = w.wCollider.suspensionSpring;
if (w.wGraphic.name == "Front Wheel")
js.spring = springForceFront;
else
js.spring = springForceRear;
js.damper = damperForce;
js.targetPosition = targetPosition;
w.wCollider.suspensionSpring = js;
}
}
} // Awake function end
Using the nested class example above, things get a little more complicated (at least for me). That's why I'm wondering if there are benefits to using nested classes?
Thanks! Stephane
Answer by Peter G · Apr 03, 2011 at 10:49 PM
Thanks for the link Peter. After hours of reading about nested classes, I finally have a better understanding of their use. I'm still a little confused on where and when I should use them, but I think the above example i gave doesn't really need a nested class.
Your answer
![](https://koobas.hobune.stream/wayback/20220613120553im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Array of custom class objects all return the same value? 1 Answer
Calling a method of a class that is a part of another class? 1 Answer
Drawing a series of triangles using scripts 1 Answer
How to get object to have two or more same classes at script 1 Answer