- Home /
Question by
Injourn · Apr 23, 2016 at 12:20 AM ·
c#nullreferenceexceptioninheritancemethod
Null Reference Exception: Object Reference, with base class?
For some reason, i don't understand, i am getting a null reference exception when i try to inherit a method from a base class. It is with the jump function at line 53 for the Child class and line 25 for the parent class
public class Racers : MonoBehaviour {
Rigidbody2D rb2D;
protected float speed = 10f;
protected float jumpSpeed = 800;
protected float roundToOne(float lol){
if (lol > 0) return 1;
else return -1;
}
protected bool WallSitting(int layer){
RaycastHit2D bump = Physics2D.Raycast(transform.position,Vector2.right * transform.localScale.x,0.55f,~((1<<9) | (1<<8)));
if (bump.collider != null && bump.collider.gameObject.tag == "Untagged") return true;
else return false;
}
protected bool jumped;
void Start(){
rb2D = GetComponent<Rigidbody2D> ();
}
protected void Jump(int Action){
switch (Action) {
case 1:
rb2D.AddForce (Vector2.up * jumpSpeed);
rb2D.velocity = new Vector2 (rb2D.velocity.x / 2, 0);
jumped = true;
break;
case -1:
if(rb2D.velocity.y > 0.5)
rb2D.velocity = new Vector2 (rb2D.velocity.x, rb2D.velocity.y/3);
break;
case 0:
rb2D.velocity = new Vector2 (11 * -transform.localScale.x, 13);
transform.localScale = transform.localScale.x == 1 ? new Vector2 (-1, transform.localScale.y) : new Vector2 (1, transform.localScale.y);
jumped = true;
break;
}
}
protected void Direction(float JustThat){
float horiz = JustThat * speed;
transform.Translate (horiz * Time.deltaTime, 0, 0);
if (Mathf.Abs(JustThat) >= .9 && Utilities.Grounded (transform)) {
transform.localScale = new Vector2 (roundToOne(JustThat), transform.localScale.y);
}
}
protected void Animator(){
GetComponent<Animator>().SetFloat("Speed",Mathf.Abs(1));
GetComponent<Animator>().SetBool("HitSpace",jumped);
GetComponent<Animator>().SetBool("grnded",Utilities.Grounded (transform));
GetComponent<Animator>().SetFloat("AirSpeed",rb2D.velocity.y);
GetComponent<Animator>().SetBool("OnWall",WallSitting(3));
jumped = false;
}
}
Child Class:
public class VirtualController : Racers {
public bool Recording;
public List<PressTime> Controller = new List<PressTime> ();
float dir;
int action;
int arraycheker(int testnum){
if (testnum < 0)
return 0;
else
return testnum;
}
void Start(){
if(!Recording)
StartCoroutine (Playback());
}
void Update(){
if (Recording)
Record ();
else
transform.Translate (10f * Time.deltaTime * dir,0,0);
}
void Record(){
if(Input.GetKeyDown("a") || Input.GetKeyUp("a"))
Controller.Add(new PressTime("a",Time.time));
if(Input.GetKeyDown("d") || Input.GetKeyUp("d"))
Controller.Add(new PressTime("d",Time.time));
if(Input.GetKeyDown(KeyCode.Space) || Input.GetKeyUp(KeyCode.Space))
Controller.Add(new PressTime("space",Time.time));
}
IEnumerator Playback(){
int counter = 0;
int i = 0;
bool a = false;
bool d = false;
bool space = false;
while (counter < Controller.Count - 1) {
if (Controller [i].buttonPressed == "a") {
dir = a ? 0 : -1;
a = a ? false : true;
Debug.Log ("a action");
} else if (Controller [i].buttonPressed == "d") {
dir = d ? 0 : 1;
d = d ? false : true;
Debug.Log ("d action");
} else if (Controller [i].buttonPressed == "space") {
space = space ? false : true;
action = action == 1 ? 0 : 1;
base.Jump (1);
}
yield return new WaitForSeconds (Controller [i].Time - (Controller[arraycheker(i-1)].Time));
i++;
}
}
[System.Serializable]
public struct PressTime{
public float Time;
public string buttonPressed;
public PressTime(string button, float tim){
buttonPressed = button;
Time = tim;
}
}
}
Comment
Best Answer
Answer by Injourn · Apr 23, 2016 at 12:45 AM
Nevermind i figured it out
I had to add
void Start(){
base.Start();
}
To Virtual controller.
This didn't work with me. The version of my Unity is 5.6.1. I did this ins$$anonymous$$d:
public class Checker : $$anonymous$$onoBehaviour {
protected Health health;
protected virtual void CheckViolation (Vehicle vehicle) { }
protected void Initialize () {
health = GameObject.Find("Health").GetComponent<Health> ();
}
void Start () {
Initialize ();
}
}
The inherited class:
public class LaneEnd : Checker {
// Use this for initialization
void Start () {
base.Initialize ();
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612075418im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
An OS design issue: File types associated with their appropriate programs 1 Answer
Variables are assigned but it returns null 2 Answers
Multiple Cars not working 1 Answer
Inheritance and this NRE 1 Answer
Distribute terrain in zones 3 Answers