Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 · Aug 05, 2011 at 04:27 AM · classinheritancenestedpolymorphism

Public variables from another class's nested class

So I have a question about using public variables from an inherited class's nested class. Here's my base class:

 using UnityEngine;
 using System.Collections;
 
 public class CameraController : MonoBehaviour {
     
     // Singleton access
     private static CameraController _instance;
     public static CameraController Instance
     {
         get { return _instance; }
     }
 
     // Create a new class defining variables for the default camera movements...
     public class DefaultCamMovements {
         public float camOffsetX     = 5.5f;         // Cam offset on the X axis (forward & backward).
         public float camOffsetY     = 2.5f;         // Cam offset on the Y axis (up & down).
         public float camOffsetZ     = -30.0f;         // Cam offset on the Z axis (zoom).
         public float maxCamPosZ     = -160.0f;        // Max cam Z position (max zoom amount).
         public float rightDamping     = 13.5f;        // Amount of dampening (smoothness of movement) when moving forward.
         public float leftDamping     = 20.0f;        // Amount of dampening (smoothness of movement) when moving backward.
         public float upDamping         = 1.5f;            // Amount of dampening (smoothness of movement) when moving up.
         public float downDamping     = 13.0f;        // Amount of dampening (smoothness of movement) when moving down.
         public float groundDamping     = 8.0f;            // Amount of dampening (smoothness of movement) when landing.
         public float zoomDampingUp     = 0.25f;        // Amount of zoom dampening (smoothness of movement) when bike is jumping up.
         public float zoomDampingDown     = 1.0f;        // Amount of zoom dampening (smoothness of movement) when bike is coming down.
         public float zoomDampingGround     = 3.0f;        // Amount of zoom dampening (smoothness of movement) when bike is grounded.
         public float rayLenghtDown         = 30.0f;    // Lenght of the ray used to detect ground collision while airborne.
         public float rayLenghtRight     = 15.0f;    // Lenght of the ray used to detect forward collision.
     }
     public DefaultCamMovements camMov = new DefaultCamMovements();
 }


Then I have another class attached to a GameObject, which inherits from CameraController:

 using UnityEngine;
 using System.Collections;
 
 public class SideCam_3D : CameraController {
     
     // Define the transform this script is attached to, and cache it on Awake for speed improvements
     private Transform thisTransform;
     
     
     // Define tunable variables
     public float camOffsetX;             // Cam offset on the X axis (zoom).
     public float camOffsetY;             // Cam offset on the Y axis (up & down).
     public float camOffsetZ;             // Cam offset on the Z axis (forward & backward).
     public float maxCamPosZ;            // Max cam Z position (max zoom amount).
     public float rightDamping;            // Amount of dampening (smoothness of movement) when moving forward.
     public float leftDamping;            // Amount of dampening (smoothness of movement) when moving backward.
     public float upDamping;                // Amount of dampening (smoothness of movement) when moving up.
     public float downDamping;            // Amount of dampening (smoothness of movement) when moving down.
     public float groundDamping;            // Amount of dampening (smoothness of movement) when landing.
     public float zoomDampingUp;            // Amount of zoom dampening (smoothness of movement) when bike is jumping up.
     public float zoomDampingDown;        // Amount of zoom dampening (smoothness of movement) when bike is coming down.
     public float zoomDampingGround;        // Amount of zoom dampening (smoothness of movement) when bike is grounded.
     public float rayLenghtDown;            // Lenght of the ray used to detect ground collision while airborne.
     public float rayLenghtRight;        // Lenght of the ray used to detect forward collision.
     
     public float velocityTresholdX;        // If target's speed is bigger then this, start zooming out.
     
     private bool getRelativePos;
     
 
     
     void Start () {
         
         // Cache the transform of this gameObject for speed
         thisTransform = this.transform;
         
         // Link all the variables from main script.
         camMov.camOffsetX = camOffsetX;
         camMov.camOffsetY = camOffsetY;
         camMov.camOffsetZ = camOffsetZ;
         camMov.maxCamPosZ = maxCamPosZ;
         camMov.rightDamping = rightDamping;
         camMov.leftDamping = leftDamping;
         camMov.upDamping = upDamping;
         camMov.downDamping = downDamping;
         camMov.groundDamping = groundDamping;
         camMov.zoomDampingUp = zoomDampingUp;
         camMov.zoomDampingDown = zoomDampingDown;
         camMov.zoomDampingGround = zoomDampingGround;
         camMov.rayLenghtDown = rayLenghtDown;
         camMov.rayLenghtRight = rayLenghtRight;
         
         orthoCam.velocityTresholdX = velocityTresholdX;
         
     }
     
     void LateUpdate () {
         
         if( GameStates.initDone != true )
         {
             //Debug.LogWarning("This is LateUpdate in SideCam_3D.\n " +
             //"GameStates Initialization isn't done yet, so we're not starting the Camera functions.");
             return;
         }
         else
         {
             // Temporary Link all the variables from main script to the local ones so we can test
             // them while running the game. Once numbers locked, remove the following references from LateUpdate
             camMov.camOffsetX = camOffsetX;
             camMov.camOffsetY = camOffsetY;
             camMov.camOffsetZ = camOffsetZ;
             camMov.maxCamPosZ = maxCamPosZ;
             camMov.rightDamping = rightDamping;
             camMov.leftDamping = leftDamping;
             camMov.upDamping = upDamping;
             camMov.downDamping = downDamping;
             camMov.groundDamping = groundDamping;
             camMov.zoomDampingUp = zoomDampingUp;
             camMov.zoomDampingDown = zoomDampingDown;
             camMov.zoomDampingGround = zoomDampingGround;
             camMov.rayLenghtDown = rayLenghtDown;
             camMov.rayLenghtRight = rayLenghtRight;
             
             orthoCam.velocityTresholdX = velocityTresholdX;
             
             
             if( this.camera.enabled )
                 getRelativePos = true;
             else
                 getRelativePos = false;
             
             
             // Invoke the CamFollow function. 
             // Pass "target" for the object to follow and "transform" for the camera it's attached to.
             SmoothCamFollow( Managers.BikeObjCache.transform, thisTransform, getRelativePos );
             
         }
     }
     
 }


That's the only way I have found so far to make those variables public and appear in the inspector...is there another (easier, cleaner) way to achieve this? I want to create a new instance of the nested class for each camera, instead of using the base nested class directly - in which case I would just use [System.Serializable] to show the class members in the inspector.

Thanks for your time!

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
0

Answer by roamcel · Aug 05, 2011 at 09:29 AM

Depending on the scope and extension of your CameraController original class, you might be able to solve the "wrapper" issue by creating your CameraController class as STATIC. This way, you surely will have to declare all your SideCam_3D public variables, but your method calls would be in the form of

 mysidecam_3dvalue = CameraController.mystaticmethod();

which comes from its script

 using UnityEngine;
 using System.Collections;

 public static class CameraController {
   your structs, functions, etc. here
 }

saving you the need to declare and assign the CameraController variables, and rather have them as function parameters, or even assigning them to the class in a constructor.

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 · Aug 06, 2011 at 07:57 AM 0
Share

Thanks for your response roamcel. I did a quick test with what you suggested, but unfortunately it doesn't work the way I need it to work.

Thanks anyways

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

An OS design issue: File types associated with their appropriate programs 1 Answer

How can I access an inherited method from a separate (collided) object? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Polymorphism question for my custom class 1 Answer

How to build up the class structure for an object 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