Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Jordi · Apr 06, 2012 at 01:13 PM · inheritanceclassessubclassing

Inheritance from object to object

I have been working with inheritance in classes for the first time now and some things confuse me. Say I define some variables at the top of the class, give them some value in the Start() function and use them in a sub-class. Sometimes my variables cannot be found in the subclass.

For example, in the base class I type this:

 using UnityEngine;
 using System.Collections;
 
 public class GeneralItemScript : MonoBehaviour {
     
     public InteractionScript iaScript;
     public GameObject player;
     
     // Use this for initialization
     void Start () {
         player = GameObject.Find ("Player");
         
         iaScript = player.GetComponent<InteractionScript>();
         
     }
     
     // Update is called once per frame
     void Update () {
     
     }
     
     
     virtual public void PerformInteraction() {
         
     }
 }

And in the subclass I attempt to use this iaScript in a custom function ( PerformInteraction() ):

 using UnityEngine;
 using System.Collections;
 
 public class PickupItemScript : GeneralItemScript {
     
     public bool b_isCarried;
     
     // Use this for initialization
     void Start () {
     
         b_isCarried = false;
         
     }
     
     // Update is called once per frame
     void Update () {
         
     }
     
     override public void PerformInteraction() {
         if (iaScript.SetCarriedItem(this.gameObject)) {
             b_isCarried = true;
         }
     }
     
     
     public void SetCarryLocation() {
         transform.parent = player.transform.Find("Arm");
     }
     
     public void StopCarry() {
         Vector3 tempVec = transform.position;
         transform.parent = null;
         transform.position = tempVec;
         b_isCarried = false;
     }
 }


This earns me the error that I cannot find the object referenced. What are the general rules/ do's and don'ts of inheritance? And how does overriding functions work exactly?

EDIT: added entire scripts

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 Kryptos · Apr 06, 2012 at 01:59 PM 0
Share

Your code snippets doesn't show anything related to inheritance. We can't help you without it.

Please provide us at least with base class definition, inherited class definition (you can remove everything not related to your issue if those classes are big files) and context call for method PerformInteraction.

avatar image Jordi · Apr 06, 2012 at 02:04 PM 0
Share

I hope this will provide more information?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Kryptos · Apr 06, 2012 at 02:10 PM

Your Start() method isn't properly overridden. Therefore the base implementation is never called. That is why your iascript remains null.

 // in GeneralItemScript.cs
 virtual protected void Start()
 {
     player = GameObject.Find ("Player");
     iaScript = player.GetComponent<InteractionScript>();
 }

 // in PickupItemScript.cs
 override protected void Start()
 {
     base.Start();
     b_isCarried = false;
 }


More about it (MSDN):

override (C# Reference)

base (C# Reference)

Comment
Add comment · Show 3 · 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 Jordi · Apr 10, 2012 at 11:35 AM 0
Share

I keep receiving the same error. $$anonymous$$y script looks the same and I changed these lines you gave me, but I still get this:

NullReferenceException: Object reference not set to an instance of an object PickupItemScript.PerformInteraction () (at Assets/Scripts/Item Scripts/PickupItemScript.cs:23) InteractionScript.AttemptInteraction () (at Assets/Scripts/InteractionScript.cs:52) InteractionScript.Update () (at Assets/Scripts/InteractionScript.cs:31)

avatar image Bunny83 · Apr 10, 2012 at 02:32 PM 0
Share

@Jordi: $$anonymous$$eep in $$anonymous$$d that Start() is called the next frame after you create the monobehaviour. If you try to access the object in the same frame it won't work.

You could use Awake() ins$$anonymous$$d, but keep in $$anonymous$$d that Awake is called from the internal constructor of the class. It might be too early

avatar image Kryptos · Apr 10, 2012 at 04:05 PM 0
Share

@Jordi: Awake can also be overidden. Try my code sample changing Start to Awake.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Class extends Object but does not inherit variables 1 Answer

Abstract class question 1 Answer

Defining and inheriting from a Javascript Class 0 Answers

Can't inherit from namespace 0 Answers

Is Instance Of 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