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 tewilliger666 · Jun 12, 2019 at 12:16 AM · rigidbody2dparentchildcomponent

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

Comment
Add comment · Show 2
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 MadDevil · Jun 12, 2019 at 02:54 AM 0
Share

if your player2 is child of character class you have you use getcomponent in children.

 Rigidbody2D rd2 = GetComponentInChildren<Rigidbody2D>();
avatar image tewilliger666 · Jun 12, 2019 at 03:39 AM 0
Share

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

0 Replies

· Add your reply
  • Sort: 

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

108 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

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


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