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
1
Question by Oliver1135 · Jul 07, 2014 at 03:44 PM · referencefindvalue

Helper properties get reference to player position

What im trying to do is create a standard static "helper" class that tracks the most used variables in my project so that I can find them quickly and easily, the first one i'm trying to setup is a way to get the player's current position in the gameworld since it would be used often for enemy 'AI', i thought I would be able to do this with the following code.

basically I want the following:

 private static Vector3? _playerPosition;
     public static Vector3? playerPosition
     {
         get 
         {
             if (_playerPosition == null)
             {
                 _playerPosition = GameObject.FindWithTag ("Player").transform.position;
             }
             return _playerPosition;
         }
     }

if _playerPosition is nothing then use the expensive GameObject.FindWithTag to get a reference to the players position and then from now on when I need to retrieve this position then use the reference I have with _playerPosition instead of having to use the more expensive GameObject.FindWithTag function every single time I just need to get the players position.

unfortunately I only seem to get the hard value of the Vector3 the first time I call the GameObject.FindWithTag.

is there a way I can get the reference to the player's position to do what I want here instead of what appears to be a copy of the value at that instant

The Red Triangle is Stationary The Green Triangle has moved position but there is no reported change when I do a debug.log(globals.PlayerPosition) after moving to a different location

(I am certain the call to GameObject.FindWithTag("Player") is correct as when I call it every time instead of trying to store the reference in the private field, the variable updates to reflect the correct current player position every time I request the current position to output to console)

Thanks

The Red Triangle is Stationary The Green Triangle has moved position

greentriangleposition.png (76.1 kB)
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

2 Replies

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

Answer by meat5000 · Jul 07, 2014 at 03:19 PM

Use GetComponent to cache the transform you are trying to read. You can access the up to date info after this with the dot operator.

 _playerTransform = GameObject.FindWithTag ("Player").GetComponent(transform);
 
 Debug.Log(_playerTransform.position);

Perform the GetComponent once for each object.

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 Oliver1135 · Jul 07, 2014 at 03:23 PM 0
Share

That worked! can you make that as an answer so I can give it the thumbs up.

avatar image meat5000 ♦ · Jul 07, 2014 at 03:45 PM 0
Share

No problem :)

avatar image Oliver1135 · Jul 07, 2014 at 03:49 PM 1
Share

Working code:

 private static Transform _playerTransform;
 public static Transform PlayerTransform
 {
     get 
     {
         if (_playerTransform == null)
         {
             _playerTransform = GameObject.FindWithTag ("Player").GetComponent<Transform>();
                 
         }
         return _playerTransform;
     }
 }

and just using PlayerTransform.position to get the updated position

avatar image
0

Answer by Sigmac13 · Jul 25, 2017 at 10:18 PM

That answer is amazing! Works perfect. Thanks for the Working code too!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Launching local Unity API Reference from MonoDevelop 7 Answers

Using C# how can I setup a "database" script? 1 Answer

creating reference via script 2 Answers

transform.Find(string)? 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