• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

188 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 avatar image

Related Questions

OnCollisionEnter2D/OnCollisionStay2D only detecting collision with the ground 1 Answer

Setting GameObject in a script. 0 Answers

(noob) Weapons in 2D game? 1 Answer

2d collision according to sprite image 1 Answer

How do you change a variable in a script, that is on an object instantiated from a prefab? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges