- Home /
Parent class cannot find component
Hi All, beginner here. Please be gentle.
I am following a tutorial series on youtube and have hit a snag. The parent class (Character) seems unable to get the childs class' Rigidbody2D and fails to set it's velocity.
The rigidbody2d component is deinitely there and I can't seem to find any typos...so I'm a bit miffed.
The error being thrown is a null reference exception directed at Move(), where the velocity is set.
Here is the parent class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class Character : MonoBehaviour
{
[SerializeField]
private float speed2 = 5f;
//animation code
//private Animator animator;
protected Vector2 direction;
private Rigidbody2D rb2d;
// Start is called before the first frame update
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
}
protected virtual void Update()
{
}
private void FixedUpdate()
{
Move();
}
public void Move()
{
//transform.Translate(direction * speed2 * Time.deltaTime);
rb2d.velocity = direction * speed2; //null reference exception thrown here
}
}
Here is the player script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player2 : Character
{
void Start()
{
direction = Vector2.up;
}
protected override void Update()
{
GetInput();
base.Update();
}
private void GetInput()
{
direction = Vector2.zero;
if (Input.GetKey(KeyCode.W))
{
direction += Vector2.up;
}
if (Input.GetKey(KeyCode.A))
{
direction += Vector2.left;
}
if (Input.GetKey(KeyCode.S))
{
direction += Vector2.down;
}
if (Input.GetKey(KeyCode.D))
{
direction += Vector2.right;
}
}
}
To recap, the Character class cannot find the Rigidbody2D component of the Player2 class. This prevents setting of the Rigidbody2D velocity and therefore prevents player movement (and I cannot locate what the issue could possibly be).
Cheers
if your player2 is child of character class you have you use getcomponent in children.
Rigidbody2D rd2 = GetComponentInChildren<Rigidbody2D>();
To be sure I'm not mixing terms (as I'm pretty fresh in OOP world).....the child (Player2) inherits from the parent class (Character)....yes? This is the way the tutorial appears to implement it so I'm deter$$anonymous$$ed to make it work :/ Converting the parent Start()
protected virtual void Start()
and the child Start() to
protected override void Start()
seems to have fixed the issue (with baseStart() being called in Player2 Start()). Is there any reason as to why Start() should not be exposed like this? Here is the URL for the video in particular: https://www.youtube.com/watch?v=Y03jBu6enf8&list=PLX-uZV$$anonymous$$_0$$anonymous$$_6JEecbu3Y-nVnANJznCzix∈dex=8
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
How can I access a component in a parent object? 1 Answer
Clicking on child executes parent script when Rigidbody2D present? 2 Answers
How to move your player with a moving platform (parent-child method not working). 2 Answers
Decrease rigidbody2D velocity per object attached to parent 1 Answer