- Home /
I can't make my sub class inherit from my base class [Mac + MonoDevelop]
Im currently building some UI elements in Unity, and I have different scripts on various elements in the UI. I have the following classes in two separate files:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class loadTxt : MonoBehaviour {
public string textFile = "text";
string textContent;
List<string> productList;
private string[] productArray;
void Start () {
TextAsset txtAsset = (TextAsset)Resources.Load (textFile);
textContent = txtAsset.text;
productArray = textContent.Split (';');
productList = new List<string> ();
for (int i = 0; i < productArray.Length; i++) {
productList.Add (productArray [i]);
}
}
void RemoveFromList(int index) {
productList.RemoveAt (index);
}
public string TextContent {
get { return this.textContent;}
set { textContent = value; }
}
void OnGUI () {
for (int i = 0; i < productList.Count; i++) {
GUILayout.Label (productList[i]);
}
}
}
class onClickRemove : loadTxt {
textContent = "new_value";
public void onClick() {
TextContent = "other_value";
}
}
For some reason I can't inherit any variables or functions in my sub class "onClickRemove" from my base class "loadTxt". If I type in a change the value of the variable "textContent" in my sub class, the program does not autocomplete the function name and if I type in the change anyway I get an error saying the variable does not belong in that content, which basically means the sub class has not inherited the values from its base class.
What am I missing to get it to work, Ive searched to find an explanation to this problem but no fix have worked so far.
what is the purpose of textContent = "new_value" in loadTxt? since textContent is private it cannot be accessed at this point
Answer by antx · Apr 18, 2016 at 07:41 AM
Your subclass has "textContent" without a type. "textContent" in your base class is not public nor protected. So your subclass does not see it.
Also class and function names should start with a capital letter.
Arh I feel stupid, O$$anonymous$$ I thought that the privacy declaration on variables and functions only had to do with accessing its value from external applications such as Unity e.g
Thank you!
Your answer
![](https://koobas.hobune.stream/wayback/20220612075032im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why use inheritance? 3 Answers
Inheritance and using GetComponent 2 Answers
Unity exports my derived class, but not its base class (C# OOP) - Is there a workaround? 0 Answers
Create a new script object from a class which inherits from MonoBehavior 4 Answers
Won't excecute the overwrited function 3 Answers