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

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 To Make An Object Appear In Front Of Player Without Cloning? 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