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 /
  • Help Room /
avatar image
0
Question by chadfranklin47 · Mar 04, 2016 at 08:38 PM · variablesclassinheritancetypeconfig

How can I access both classes from a single type?

Hello, I have a camera script that I wish to have multiple configurations for different modes. For example, default and aim. I want the main camera script to read the config from one of two classes, the default class, or the aim class. In the camera script for example, I would have x = mouse.x * config.rotationSensitivity;. Since the config is in two different classes, I added them to the same script so that I could make references to them from the monobehaviour class at the top. So there are now 3 classes. I want to be able to switch between what config class I am using as the config, but I don't know what type to make the config variable to be able to hold both classes and access their variables. Would I have to use a different method of holding the variables like a list? Here are parts of the camera script:

 public ? config;
 
         protected virtual void Start () {
             thirdPersonCameraConfig = GetComponent<ThirdPersonCameraConfig> ();
             config = thirdPersonCameraConfig.defaultConfig;
             x += mouse.Value.x * config.rotationSensitivity;
         }

Here is the config script:

 public class ThirdPersonCameraConfig : MonoBehaviour {
 
         public DefaultConfig defaultConfig;
         public AimConfig aimConfig;
     }
 
     [System.Serializable]
     public class DefaultConfig {
         public float rotationSensitivity = 3.5f; // The sensitivity of rotation
     }
 
     [System.Serializable]
     public class AimConfig {
         public float rotationSensitivity = 2.5f; // The sensitivity of rotation
     }
 }

Any help would be awesome, Thanks.

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
1
Best Answer

Answer by EmHuynh · Mar 04, 2016 at 09:51 PM

Hello, @chadfranklin47.

Update:

We can work with a single class, it's more simple and gets the job done.

 using UnityEngine;
 using System;
 using System.Collections;
 
 [ System.Serializable ]
 public class Config
 {
     public        float    cameraRotationSensitivity;
     public        bool    isWindowMode;
 
     public Config() {
         cameraRotationSensitivity = 2.5f; // Default value.
         isWindowMode = false;
     }
 
     public static void CopyConfigAToB( Config configA, Config configB )
     {
         // Manually clone everything. There are other ways of cloning,
         // but this gives us the most control.
         configB.cameraRotationSensitivity = configA.cameraRotationSensitivity;
         configB.isWindowMode = configA.isWindowMode;
     }
 }


Implementation:

     public        Config            defaultConfig;
     public        Config            customConfig;

     void InitializeConfigs() {
         defaultConfig = new Config();
         customConfig = new Config();
 
         // Set [customConfig].
         customConfig.cameraRotationSensitivity = 5f;
         customConfig.isWindowMode = true;
     }
 
 
     void Start() {
         InitializeConfigs();
 
         // Debugging.
         Debug.Log( "Default = " + defaultConfig.cameraRotationSensitivity.ToString() );
         Debug.Log( "Custom = " + customConfig.cameraRotationSensitivity.ToString() );
         Config.CopyConfigAToB( customConfig, defaultConfig );
         Debug.Log( "Copied Custom to Default..." );
         Debug.Log( "Default is now = " + defaultConfig.cameraRotationSensitivity.ToString() );
     }


Old:


 public class ThirdPersonCameraConfig : MonoBehaviour {
     public    DefaultConfig    defaultConfig;
     public    AimConfig        aimConfig;
     
     // The sensitivity of rotation
     public    float            rotationSensitivity;
 }
 
 [ System.Serializable ]
 public class DefaultConfig : ThirdPersonCameraConfig {
     // Default constructor
     public DefaultConfig() {
         // Assign a value to a member variable
         // from base class.
         rotationSensitivity = 3.5f;
     }
 }
 
 [ System.Serializable ]
 public class AimConfig : ThirdPersonCameraConfig {
     public AimConfig() {
         rotationSensitivity = 2.5f;
     }
 }






Comment
Add comment · Show 6 · 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 chadfranklin47 · Mar 05, 2016 at 02:37 PM 0
Share

Sorry @EmHuynh I had to edit the question because I did something wrong. Could you please look at it again?

avatar image EmHuynh chadfranklin47 · Mar 05, 2016 at 09:50 PM 0
Share

Hey, @chadfranklin47. I have updated my answer. (:

avatar image chadfranklin47 EmHuynh · Mar 05, 2016 at 10:57 PM 1
Share

Thanks so much for the answer. It helped a bunch.

Show more comments
avatar image EmHuynh · Mar 07, 2016 at 09:34 AM 0
Share

No problem, @chadfranklin47. Let us know if you have any more questions!

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

48 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Inheriting values from base class in c# 0 Answers

Define attacks to choose from 1 Answer

Suggestions for planning project tasks and organizing classes/functionality? 0 Answers

Can I have a variable accessed through the inspector and be accessed by a child script without making it public? 1 Answer

One variable handling two types without a mess 2 Answers


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