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 /
  • Help Room /
avatar image
0
Question by tylersbrown01 · May 21, 2016 at 07:43 PM · inheritancereference-other-object

Access an object reference variable from parent class

Several scripts need to access the rigidbody on my two playerShip gameObjects. Re-typing GetComponent for each script I write is getting tedious, so I created a helper class called "Element" that stores references to frequently referenced objects in the scene.

 public class Element : MonoBehaviour {
     
     public GameObject leftShip;
     public GameObject rightShip; 
     protected Rigidbody leftRigid;
     protected Rigidbody rightRigid;
 
     void Start () {
         leftRigid = leftShip.GetComponent<Rigidbody>();
         rightRigid = rightShip.GetComponent<Rigidbody>();
     }
 }

I then have a child class called "PlayerInput" which applies forces to the rigidbodies of the ship.

 public class ShipInput : Element {
 
     private void MoveShip (Vector2 speedAxis) {
             leftRigid.AddForce (Vector3.right * leftShipForce * thrust * 100f);
             rightRigid.AddForce (Vector3.right * rightShipForce * thrust * 100f);
         }
     }
 }

When I play the game, I'm getting a null reference error for left rigid and right rigid variables any time the function "MoveShip" is called. Note that the two player ship gameObjects were dragged into the element class from the scene (not a prefab) and that the element class is attached to a gameObject in the scene called "LevelManager."

Is this the correct way to do this? If not, how could I store references to commonly used objects in a similar way? Thanks for reading.

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 JoshDangIt · May 21, 2016 at 08:07 PM 0
Share

Are the variables being referenced before/during the Start function? Is that the full ShipInput script?

avatar image tylersbrown01 JoshDangIt · May 21, 2016 at 08:09 PM 0
Share

I fixed it but you were on the the right track. Turns out for some reason that child never had access to it's paren'ts start function. If you read my reply below you can see what I did to solve the problem. Thanks!

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by tylersbrown01 · May 21, 2016 at 08:08 PM

I figured it out!! So what happened is the child class didn't have access to the parent's start function. So I changed the start function in the parent class to "Protected void GetComponents ()" and then called GetComponents() in the child script's start () function.

EDIT: This is actually not the best option as it is taxing and redundant for each child class to have to call the GetComponents() methods in it's awake function.

The best solution is to simply change Start() to Awake() in the parent class

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 gregor039 · Aug 31, 2019 at 11:34 AM 0
Share

Thank you! I know this question is old, but I had to sign in and thank you for the propper answer. I couldn't find a hint to this issue anywhere and just never knew that childs can't access their parents start. I guess I am not the only one so hopefully more people will see it this way!

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

42 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

Related Questions

How would I get around multiple inheritance for this instance? 1 Answer

Items/Inventory system using Scriptable Objects and Composition instead Inheritance. 0 Answers

Randomizing and using Bool values 0 Answers

How to determine class of object at mouse position 1 Answer

"I am getting a null reference exception error for Pathfollowertest.instance.centerslot.Add(Cube.transform) 0 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