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 ronronmx · Apr 03, 2011 at 10:32 PM · classeswheelcollider

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

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Peter G · Apr 03, 2011 at 10:49 PM

http://stackoverflow.com/questions/1083032/why-would-i-ever-need-to-use-c-nested-classes

Comment
Add comment · Show 1 · 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
avatar image ronronmx · Apr 04, 2011 at 05:47 AM 0
Share

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

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

No one has followed this question yet.

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


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