Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
This question was closed Dec 08, 2018 at 05:37 PM by hexagonius for the following reason:

This is a pure C# inheritance problem and is not related to Unity

avatar image
0
Question by sloththegarry · Dec 08, 2018 at 05:07 PM · inheritanceparent-childclass instance

Does attaching a derived class script to a gameobject create a new instance of its parent class specific to that gameobject?

So I have a base abstract class called CurrentAnimation and two simple derived classes that inherit from it. Each derived class is attached as a script to a gameobject (FadeOutCtrl to a Fadeout gameobject and AnimationCtrl to an Animation gameobject).

The AccessTestBool method in AnimationCtrl script is invoked using a UI Button function list by referencing the Animation gameobject. So when the button is pressed the bool in its parent class turns true. However when trying to get this bool from FadeOutCtrl class, it does not return a true value. I cannot figure out why, perhaps when attaching subclass scripts to gameobjects, they each create their own instance of the parent class? The Unity documentation isn't clear on this section.

  public abstract class CurrentAnimation : MonoBehaviour {
         private bool testBool;
         public bool TestBool // property that get's and sets testBool from outside
         {
             get
             {
                 return testBool;
             }
     
             set
             {
                 testBool = value;
             }
         }
  protected void SetTestBool(bool current) // Stores the method's parameter value in TestBool value
       {
        TestBool = current;
       }
 }



 public class FadeOutCtrl: CurrentAnimation
 {
     private bool fadeOutBool;
     private void Update()
     {
         fadeOutBool = TestBool; // Checking whether TestBool gets stored it in fadeOutBool or not
     }
 }
 
 public class AnimationCtrl : CurrentAnimation
 {
     private bool animationBool;
     void Update () 
     {
        animationBool= TestBool; //Checking whether TestBool gets stored in animationBool or not
     }
     public void AccessTestBool() // Accesses parent class' method and sets its parameter to true
     {
         SetTestBool(true);
     }
 }
Comment
Add comment · Show 7
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 Vega4Life · Dec 08, 2018 at 05:17 PM 0
Share

Are you sure you are actually calling AccessTestBool with your button. Add some debug logs in there to double check.


Also, no, nothing is instancing a base class. I think the easiest way to think about it is, public class FadeOutCtrl: CurrentAnimation is saying... 'Paste all the code of CurrentAnimation in this class for us' - but you dont see that happen.

avatar image sloththegarry Vega4Life · Dec 08, 2018 at 07:35 PM 0
Share

Yes I am in fact calling that function, the code executes and the bool turns true. But when trying to get the same variable from the other subclass (FadeOutCtrl), the value is unchanged.

avatar image sloththegarry · Dec 08, 2018 at 07:43 PM 0
Share

dear @hexagonius I don't mean to be rude but if you're not a part of the solution please don't be a part of the problem. This is in fact a unity related question since I'm trying to figure out where and when does "Unity" create instances of a class and how "Unity game objects" handle inheritance. I've spent hours reading through documentation and it didn't help. Please read the questions carefully before closing them. I've put the C# code there merely to demonstrate the issue. Thanks

avatar image hexagonius sloththegarry · Dec 08, 2018 at 07:51 PM 0
Share

I have read it carefully and it is NOT Unity related. a gameobject is a sealed class, it can not be derived from, which means it has nothing to do with handling inheritance. it's composed of components, your classes, $$anonymous$$onoBehaviours. these, in your case inherit from a base class.
read about C# (actually general OOP) inheritance, it will explain why changing the bool in one class does not change it in the other.
you could very well done the base and copy it's logic up into the deriving class, would be the same in that matter.

avatar image Vega4Life hexagonius · Dec 08, 2018 at 08:09 PM 0
Share

I don't want to cause issues either, but maybe I am confused by what both are saying. This just works. Fairly straight forward.


 using UnityEngine;
 
 public abstract class BaseClass : $$anonymous$$onoBehaviour
 {
     protected bool TestBool { get; set; }
 
     protected void SetTestBool(bool current)
     {
         TestBool = current;
     }
 }


 using UnityEngine;

 /// 
 /// Add to a gameobject and hit the space bar
 /// 
 public class DerivedClass : BaseClass
 {
     void Update()
     {
         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
         {
             SetTestBool(true);

             // I find it silly to make a call like this as you have direct access to the TestBool
             // I guess the property could be private set for whatever reason
             //TestBool = true;
         }

         Debug.Log(TestBool);
    }
 }


Add the derived class to an object in the scene and hit spacebar. It goes from false to true.

Show more comments
Show more comments

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

99 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 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 avatar image avatar image avatar image

Related Questions

How would I handle having one instance of 2 possible class, one derived from the other in a Monobehavior? 1 Answer

inheriting a class, but wanting it to instantiate on each gameobject it's applied to? 2 Answers

Saving class data without having lots of references? 1 Answer

Inheritance suddenly stopped working. 2 Answers

How to choose base class to make Inherit C# 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