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 /
avatar image
0
Question by owen_unity55 · Aug 06, 2018 at 04:00 PM · 2dscripting beginnernoobpositions

Cannot get position of other objects

I am trying to get the position of the player each frame from another script but using public GameObject player and then going in the editor and trying to change the player value to the player in scene did not work and I had to copy the player to the assets folder and then move it there. Annoyingly enough though, this gave (0, 0), (I'm working in 2D), because the object in the assets folder would not update its position. I know that the answer is probably really obvious but I am probably the newest you can get with Unity, I don't even have any other experience in C#.

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
2
Best Answer

Answer by Legend_Bacon · Aug 06, 2018 at 04:13 PM

Hello there,
"moving the player into the assets folder" in Unity, creates a prefab of that object. A prefab is used to quickly instantiate pre-set objects, that already have their own components, values etc. (For example, in an RPG you would have a goblin prefab, and create X instances of that prefab instead of making a brand new goblin every time).


If you reference your prefab in your script, that means you're getting the position of the PREFAB every frame, instead of your player in the game scene. To fix that, either assign your player (the one in the scene, not the assets folder) to your script manually, or have your program find it at the beginning.


Here's an example:

     public GameObject go_player = null;
 
     //This is called ONCE automatically on any Monobehaviour Object, at the beginning
     private void Start()
     {
         if (go_player == null)
             go_player = GameObject.FindGameObjectWithTag("MyPlayer");
     }
 
     //This is called EVERY FRAME automatically on any Monobehaviour Object
     private void Update()
     {
         Debug.Log("Player POS: " + go_player.transform.position.ToString());
     }


The only thing left to do is to give your player object the tag "MyPlayer", so your program can find it. For that, click on your object, then in the top-left of the inspector hit the "Tag" dropdown. You can then add a new tag, and give it to your player.


I hope that helps!

Cheers,

~LegendBacon

Comment
Add comment · Show 2 · 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 owen_unity55 · Aug 06, 2018 at 04:44 PM 0
Share

Thanks so much now I can continue on my game again, I didn't even know prefabs existed.

Also, ONE THOUSAND THREE HUNDRED TEN REPUTATION??????? HOW

avatar image Legend_Bacon owen_unity55 · Aug 06, 2018 at 04:47 PM 0
Share

Hey there, thank you for accepting my answer. I'm glad I could help!

If you're impressed with 1000 points, wait until you see some of the true experts on this page...

avatar image
1

Answer by MT369MT · Aug 06, 2018 at 04:07 PM

Hi, player is only a variable. Before using it you must assign it. You can do it in the inspector (drag and drop the player in your "none (GameObject)" field or you can assign it in script for example:

 public GameObject player;
 public Vector2 playerPosition;
 
 void Start ()
 {
 player = GameObject.Find("Player");
 }
 
 void Update ()
 {
 playerPosition = player.transform.position;
 }
 

This will assign the variable player with the object in the scene called "Player". Ask if you need something else.

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 owen_unity55 · Aug 06, 2018 at 04:44 PM 0
Share

Thanks for the answer but no offense Legend_Bacon made his/her answer cleaner so I accepted his/hers.

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

187 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 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 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 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 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

Trouble with pathfinding in 2d 0 Answers

Need help with creating a 2D polygon from a series of GameObjects in C# 3 Answers

2D Animation does not start 1 Answer

How to make a companion that trails character moves, and not collide? 0 Answers

Need help writing script 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