I have been working with inheritance in classes for the first time now and some things confuse me.
Say I define some variables at the top of the class, give them some value in the Start() function and use them in a sub-class. Sometimes my variables cannot be found in the subclass.
For example, in the base class I type this:
using UnityEngine;
using System.Collections;
public class GeneralItemScript : MonoBehaviour {
public InteractionScript iaScript;
public GameObject player;
// Use this for initialization
void Start () {
player = GameObject.Find ("Player");
iaScript = player.GetComponent<InteractionScript>();
}
// Update is called once per frame
void Update () {
}
virtual public void PerformInteraction() {
}
}
And in the subclass I attempt to use this iaScript in a custom function ( PerformInteraction() ):
using UnityEngine;
using System.Collections;
public class PickupItemScript : GeneralItemScript {
public bool b_isCarried;
// Use this for initialization
void Start () {
b_isCarried = false;
}
// Update is called once per frame
void Update () {
}
override public void PerformInteraction() {
if (iaScript.SetCarriedItem(this.gameObject)) {
b_isCarried = true;
}
}
public void SetCarryLocation() {
transform.parent = player.transform.Find("Arm");
}
public void StopCarry() {
Vector3 tempVec = transform.position;
transform.parent = null;
transform.position = tempVec;
b_isCarried = false;
}
}
This earns me the error that I cannot find the object referenced.
What are the general rules/ do’s and don’ts of inheritance? And how does overriding functions work exactly?
EDIT: added entire scripts