- Home /
Inheritance from object to object
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
Your code snippets doesn't show anything related to inheritance. We can't help you without it.
Please provide us at least with base class definition, inherited class definition (you can remove everything not related to your issue if those classes are big files) and context call for method PerformInteraction.
Answer by Kryptos · Apr 06, 2012 at 02:10 PM
Your Start()
method isn't properly overridden. Therefore the base implementation is never called. That is why your iascript
remains null
.
// in GeneralItemScript.cs
virtual protected void Start()
{
player = GameObject.Find ("Player");
iaScript = player.GetComponent<InteractionScript>();
}
// in PickupItemScript.cs
override protected void Start()
{
base.Start();
b_isCarried = false;
}
More about it (MSDN):
I keep receiving the same error. $$anonymous$$y script looks the same and I changed these lines you gave me, but I still get this:
NullReferenceException: Object reference not set to an instance of an object PickupItemScript.PerformInteraction () (at Assets/Scripts/Item Scripts/PickupItemScript.cs:23) InteractionScript.AttemptInteraction () (at Assets/Scripts/InteractionScript.cs:52) InteractionScript.Update () (at Assets/Scripts/InteractionScript.cs:31)
@Jordi: $$anonymous$$eep in $$anonymous$$d that Start() is called the next frame after you create the monobehaviour. If you try to access the object in the same frame it won't work.
You could use Awake() ins$$anonymous$$d, but keep in $$anonymous$$d that Awake is called from the internal constructor of the class. It might be too early
@Jordi: Awake
can also be overidden. Try my code sample changing Start
to Awake
.
Your answer
Follow this Question
Related Questions
Class extends Object but does not inherit variables 1 Answer
Abstract class question 1 Answer
Defining and inheriting from a Javascript Class 0 Answers
Can't inherit from namespace 0 Answers
Is Instance Of 3 Answers