• 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 /
  • Help Room /
avatar image
0
Question by Anthonyknowles · Feb 22, 2017 at 11:57 AM · gameobjecttransformpositionmovetowardsfindwithtag

Cannot properly access player position

I have been trying to make an enemy script that cause the enemy to move towards the player. As far as i know i'm accessing the player object properly but my position variable always returns 0,0 which i suspect is null. My enemy object always moves towards 0,0,0 below is my script please could you have a look and see what i'm doing wrong? I'm new so please try and explain why I'm wrong as well but if you don't have the time, thanks anyway :)

PS: My player object does have the tag "Player" on top level and has a transform attached

 using UnityEngine;
 using System.Collections;
 
 public class enemyControllerA : MonoBehaviour {
 
 
     public GameObject target;
     public float speed;
     Vector2 playerPos;
     void Start () {
         target = GameObject.FindWithTag ("Player");
     }
     
     // Update is called once per frame
     void Update () {
         playerPos = new Vector2(target.transform.position.x,target.transform.position.y);
         float step = speed * Time.deltaTime;
         transform.position = Vector2.MoveTowards (transform.position,playerPos,step);
         print (playerPos);//always returns 0,0?? why god why!!!!
 
     }
 }


alt text

screen-shot-2017-02-22-at-32743-pm.png (75.2 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Zee-Play · Feb 22, 2017 at 12:13 PM

Make sure you have your "Player" tag created and assigned your player object that tag.

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 Anthonyknowles · Feb 22, 2017 at 12:18 PM 0
Share

Read just above my code

avatar image Zee-Play Anthonyknowles · Feb 22, 2017 at 12:55 PM 1
Share

Well, I ran your script and it works for me, that's why I assume the problem might be in the inspector.

avatar image Anthonyknowles Zee-Play · Feb 22, 2017 at 01:27 PM 0
Share

I have read in a few places that it is a bug and an asset reimport might help but it didnt - the only thing i have recently changed was add animations to player, although i dont see that affecting it

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

119 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

Related Questions

Vector3.MoveTowards moving transform 0 Answers

How to find the transform position of another gameobject then move a gameobject to that position? 1 Answer

When hitting border teleport bullet to player. 1 Answer

How to get the position of a gameObject based on its index in an array? 0 Answers

How can I make a ball roll when using Vector3.MoveTowards? 0 Answers

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