Question by
LegitTeddyBears · Aug 11, 2017 at 07:20 AM ·
variablesclassinheritance
Inheriting values from base class in c#
So I am trying to get value from my base class to my derived class without getting a component of the base class and using its variables. I made a basic script that I using to figure this out but so far i am unsuccessful.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BaseClass : MonoBehaviour {
public int Amount_of_bubbles;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
Amount_of_bubbles++;
}
}
My goal is to have the console display the amount of bubbles that the Base class has.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DerivedClass : BaseClass {
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
Amount_of_bubbles = base.Amount_of_bubbles;
Debug.Log(Amount_of_bubbles);
}
}
Comment
You have to explicitely call base.Update
void Update ()
{ base.Update(); Debug.Log(Amount_of_bubbles); }
Thank you! does this work for variables defined in the editor or only if the variable was defined in a method?