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 Sooper1337 · Jul 01, 2012 at 08:23 PM · c#parentchildinheritancederived-class

C# Inheritance Problems

Im trying to pull variables and their values from the parent script into the child script, but i keep getting these errors.

Assets/Scripts/shieldControl.cs(15,51): error CS0120: An object reference is required to access non-static member UnityEngine.Component.transform' > > > Assets/Scripts/shieldControl.cs(17,34): > error CS0120: An object reference is > required to access non-static member > PlayerControl.playerShieldActive'

Here is the Current Code for the Child Script

I am trying to pull the transform.position of the parent object for placement of the child object when it is called, and i am trying to pull the bool from the parent as a check on whether the object is being called.

 using UnityEngine;
 using System.Collections;
 
 public class shieldControl : PlayerControl {
     
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         
         Vector3 shieldPos = transform.position;
         Vector3 playerPos = PlayerControl.transform.position;
         
         if(PlayerControl.playerShieldActive == true){
             shieldPos = playerPos;
         }
         else{
             shieldPos = new Vector3(100f,100f,100f);
         }
         
     }
 }
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
0
Best Answer

Answer by whydoidoit · Jul 01, 2012 at 08:34 PM

When you inherit like that there is no need to refer to PlayerControl - you already have all of its variables right there.

 e.g.  Vector3 playerPos = transform.position;
Comment
Add comment · Show 7 · 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 Sooper1337 · Jul 01, 2012 at 08:47 PM 0
Share

it fixed the errors but its not updating the variables. playerShieldActive stays false, even when it is true in the parent script.

avatar image whydoidoit · Jul 01, 2012 at 08:51 PM 0
Share

You've got to have something else wrong - that would work if the base class had its value true.

Are you sure that it is true in the instance of this object? Subclasses just share definitions, not values - if you have something else which is a PlayerControl it will not change the values of something which is shieldControl.

avatar image Sooper1337 · Jul 01, 2012 at 08:52 PM 0
Share

is there any way to have shieldControl inherit the actual values from PlayerControl?

avatar image whydoidoit · Jul 01, 2012 at 08:54 PM 0
Share

No - if you wanted to have it read values from another object then you need to have a reference to that object in shieldControl and use that to get them:

    public PlayerControl playerControl;   //Set this in the inspector

    if(playerControl.playerShieldActive)


avatar image whydoidoit · Jul 01, 2012 at 08:55 PM 0
Share

I don't think you are looking for inheritance (which is the ability to have two objects share a definition of a variable or a method) - you are looking for composition - which means having references to the other things.

Show more comments

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

An OS design issue: File types associated with their appropriate programs 1 Answer

[C#] Calling a method of a child class on a parent object 1 Answer

Make a simple tree 1 Answer

How can I listen for a function being called from a parent class? 1 Answer

Inheriting Parent Rotation and position 2 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