- Home /
c# overriding an inherited field
So from what I've googled so far, my interpretation of the word "override" is not the same as is meant in c# but you'll see what I'm getting at. So I've set up three classes (scripts) like so:
using UnityEngine;
using System.Collections;
public class Basy {
public int test = 3;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
using UnityEngine;
using System.Collections;
public class Testy : Basy {
float test = 2f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
public void Update () {
Debug.Log ("test " + test);
}
}
using UnityEngine;
using System.Collections;
public class Executey : MonoBehaviour {
Testy test = new Testy ();
void Awake ()
{
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
test.Update ();
}
}
Everything runs fine, and as I expected, "2" gets debugged. My question is, is there any possible way to access the "int test = 3" field from the testy class? I made this field an int purely for testing purposes. In a situation where they would both be floats, surely the field in the class "Testy" would be technically overriding the one in the "Basy" class?
Answer by Landern · May 07, 2014 at 04:54 PM
Since it's from your base class:
base.test;
wow... and there I was thinking "I bet theres a C# keyword like "this" that works in the opposite way... and there it is. haha thanks!!!
A small not, without deriving or inheriting from $$anonymous$$onoBehavior, $$anonymous$$ethods Start, Update, FixedUpdated, OnColl....etc will not be observed by the unity engine, meaning they will not be executing unless you derive from $$anonymous$$onoBehavior.
Oh yeah I know that but I called Update () from the "executey" class so it would work in this siuation
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Make a field in inspector hold inheriting classes 2 Answers
Why is Start() from my base class ignored in my sub-class? 4 Answers
Can you override/extend text property? 3 Answers