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 HNKMaster · Jul 24, 2014 at 07:36 PM · c#variablessubclass

Getting variables from subclasses

Hi everybody. I'm having problems working with subclasses, like the next example:

I have those scripts:

.-A controller script (_thugController.cs) that manages the movement of enemies and variables (speed, jump, etc.).

.-A subclass controller script (_flyController.cs) for aereal enemies.

.-An action script (_flyActions.cs).

My "flyController.cs" script has their own variables, like ex.:

 using UnityEngine;
 using System.Collections;
 
 public class _flyController : _thugController {
 
     public Vector3 trajectory;
     
     public float accel;
     public float deccel;
     
     void Start () {
 
         base.VarStart();
     
     }
 }

And I need "_thugActions.cs" get those variables:

 public class _flyActions : _thugActions {
 
     public enum eneState{Patrol, Attack, Stop};
     public eneState state;
 
     public _thugController thugCon; //Used to access _flyController.cs
 
     // Use this for initialization
     void Start () {
 
         base.VarStart();
         state = eneState.Patrol;
         thugCon.accel = 4f;
         thugCon.deccel = 15f;
     
     }
 }

But then, the script can't get both "accel" and "deccel" because "thugCon" is being read like the Main Class, and not like the "fly" SubClass.

Is there any solution other than creating a "_flyController.cs" variable?

Comment
Add comment · Show 1
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 DajBuzi · Jul 24, 2014 at 07:41 PM 0
Share

if they both defrive from monobehaviour and are attached to the game object as a component then just use

 var myClass = GetComponent<$$anonymous$$yClassType>();

or if you want to get the parent

 var myClass = GetComponent<$$anonymous$$yClassType>() as $$anonymous$$yClassTypeParent;

just a guess ;)

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by _rob_ · Jul 24, 2014 at 08:38 PM

Based on what you are doing, changing the variable to _flyController makes the most sense.

Parent classes can't know anything about their subclasses, but sub-classes know everything the parent class knows. By declaring it as _flyController, you still have access to the functionality of _thugController. It doesn't work the other way around.

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 HNKMaster · Jul 24, 2014 at 10:35 PM 0
Share

Thanks a lot. I understood that the parent can access child vars, so I changed the declaration and it works now.

avatar image
1

Answer by duck · Jul 24, 2014 at 09:19 PM

The answer is no, and this is by design, in the c# language.

Here's the reason:

You've defined a class called "Thug", and a subclass "Fly". You've said that "Fly" has accel and decel, but not all types of Thug have those vars.

Therefore, if you have a reference type of "Thug", the compiler doesn't know which type of Thug it is, and so it cannot allow your code to assume it's a "Fly" type with the accel & decel values - so you can't refer to them.

One option would be to test if it's a "Fly" type, and re-cast it if so. Another option would be to use the specialised subclass as your variable type, as you suggested.

I'm going to show you an example of re-casting types. Your naming conventions are (to me) a bit messed up, so here's an example using unity's standard convention: Capitalised for classes, lowercase for variables. Here I define a class called "Food". It's edible by default, and has an "Eat" function.

 using UnityEngine;
 public class Food : MonoBehaviour {

     public bool edible { get; protected set; }
     protected virtual void Awake() { edible = true; }

     public void Eat() {
         if (edible) {
             Debug.Log("Yum.");
             Destroy(gameObject);
         } else {
             Debug.Log ("Yuck.");
         }
     }
 }

 

Next we have a subclass called "Meat" which starts off inedible (overriding the base function), but has a cook function to make it edible.

 using UnityEngine;
 public class Meat : Food {
     // meat is inedible until cooked
     protected override void Awake () { edible = false; }

     public void Cook() {
         edible = true;
         Debug.Log ("Cooked meat.");
     }
 }

 

Finally the "Person" class has a food variable. It wants to eat the food, but has to test to see if it's the subclass Meat (in which case it recasts the Food as Meat, and Cook()s it before eating!).

 using UnityEngine;
 public class Person : MonoBehaviour {
     public Food food;

     void Start() {
         Eat(food);
     }

     public void Eat(Food food) {
         if (food is Meat) {
             Meat meat = (food as Meat);
             meat.Cook();
         }
         food.Eat();
     }
 }
 
Comment
Add comment · 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
0

Answer by Xtro · Jul 24, 2014 at 08:05 PM

You can cast _thugController reference to _flyController and access its members...

 void Start () {
 
     base.VarStart();
     state = eneState.Patrol;

     var flyController = (_flyController)thugCon;
     flyController.accel = 4f;
     flyController.deccel = 15f;
 
 }
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 Xtro · Jul 28, 2014 at 09:44 PM 0
Share

Can you please mark the correct answer here?

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

6 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Can I choose what to pass into .getcomponent<{VARIABLE}>(); 1 Answer

Serialize Variables from Items in an Array 1 Answer

Is it possible to access a variable in a different script without reference? 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