Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pjoshua_m · Jul 05, 2017 at 09:12 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

133 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges