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 braco86 · Dec 09, 2014 at 12:48 PM · transform.positionstaticderived-class

How to get "transform.position" from Base Class in realtime

Hi, i have a Baseclass Player, and three Suclasses (Enemy, Teammate, Keeper). Every Subclass needs to know it's own unique Position with "transform.position" and the Position from the Baseclass. I found some working solutions like: unique Variables per Class (playerPos, enemyPos, etc.) or static private Variables with the name "position" in all 4 Classes with a get Property.

BUT my question is, is there a Way to declare a non-static protected Variable "position" in the Baseclass which the subclasses can use (derive) for their own Positions? And then to compare the Positions in Realtime (void Update)?.

Example:

 public class Player : MonoBehaviour {
             protected Vector3 position;
             
             void Update() {
             Position();
             }
             
             protected Vector3 Position() {
                position = transform.position;
                return position;
                }
             
             }
             
 public class Enemy : Player {
                if (base.Position().x == this.Position().x) {
                // Do something
                }
             }
         
 
 
Comment
Add comment · Show 7
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 gjf · Dec 09, 2014 at 12:29 PM 0
Share

are you trying to get some sort of parent position?

avatar image braco86 · Dec 09, 2014 at 12:32 PM 0
Share

Yes. I'm Trying to get the position from the parentclass. Is there a way unity or csharp can manage this?

avatar image RudyTheDev · Dec 09, 2014 at 12:51 PM 0
Share

Can you explain more what "subclasses can use (derive) for their own Positions" is trying to accomplish? Why can't you directly access transform.position from child classes? Are you trying to cache the position for performance?

avatar image gjf · Dec 09, 2014 at 01:03 PM 0
Share

if you want a parent, then create the new game objects and set their transform.parent to the parent object.

inheriting from a class is different from creating a parent object - if you don't actually create the parent like that, it won't have one.

avatar image braco86 · Dec 09, 2014 at 02:54 PM 0
Share

I have already Solutions for this, my question is more general. -- I want ONE Vector3 Variable (Non-Static and protected) in the Baseclass, called position. i want that my derived classes also use this Variable but for their OWN Positions. -- So Far this works normally.

Now i want to check the Position of the Class Player (Baseclass) out of the Class Enemy without creating an Instance. Is this possible?. It is possible with static Variables.

Show more comments

2 Replies

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

Answer by hexagonius · Dec 09, 2014 at 09:08 PM

If you want to access a script variable that is not related to an object (instantiation), you must define it as static, no chance to do this any other way. BUT the major problem is ONLY gameobjects, which are instances, have transforms and therefore positions. Without an instance of those, no position. The static version would also mean you instantiated only one copy, but then static would be useless, since you instantiate anyway and then you could just read its transform.position directly.

Comment
Add comment · 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
0

Answer by braco86 · Dec 10, 2014 at 12:56 PM

Thank you hexagonius! Now i get it and i am able to do that what i was looking for!

Comment
Add comment · 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

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

Have array of Unit; Unit is base Class; How to access derieved class functions??? 1 Answer

Using static methods to transform a position 1 Answer

Can't call a class variable from a class from the GUI 1 Answer

Where is the Mistake in my Coin System? 1 Answer

How to get Real-Time Light on Static Lightmap? 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