- Home /
This is a pure C# inheritance problem and is not related to Unity
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);
}
}
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.
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.
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
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.
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.
Follow this Question
Related Questions
OnTriggerStay() Not Detected In Child Class 0 Answers
An OS design issue: File types associated with their appropriate programs 1 Answer
Putting a Child Class into a List of Parent Type and then casting it back to a Child 0 Answers
inheriting a class, but wanting it to instantiate on each gameobject it's applied to? 2 Answers