• 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 NightmaresDev · Apr 21, 2017 at 10:20 PM · c#scripting problemplayertransform.positionshortcut

Getting the Player in a Script!

So what I am trying to do is that player = (Person with the tag "Player") Instead of a GameObject!
And this should be done in the Start Method. If you do not understand this it should be like this..

public Transform player;

void Start()

{
player = tag("Player")
}

This is my script if this helps!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class EnemyChase : MonoBehaviour {
 
     public float distanceToPlayer;
 
     public Transform player;
     public Animator anim;
 
     public float rotationSpeed = 2f;
 
 
     void Start ()
     {
         anim = GetComponent<Animator>();
         player = tag("Player"); // Doesnt work
     }
 
     void Update ()
     {
         Vector3 direction = player.position - this.transform.position;
         float angle = Vector3.Angle(direction, this.transform.forward);
         if (Vector3.Distance(player.position, this.transform.position) < 15 && angle < 280)
         {
             
             direction.y = 0;
 
             this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
                 Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
 
 
             anim.SetBool("IsIdle", false);
             if(direction.magnitude > distanceToPlayer)
             {
                 this.transform.Translate(0, 0, 0.16f);
                 anim.SetBool("IsWalking", true);
                 anim.SetBool("IsAttacking", false);
             }
             else
             {
                 anim.SetBool("IsAttacking", true);
                 anim.SetBool("IsWalking", false);
             }
         }
         else
         {
             anim.SetBool("IsIdle", true);
             anim.SetBool("IsWalking", false);
             anim.SetBool("IsAttacking", false);
         }
     }
 
     
 }
 

Thanks for the Help!

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

Answer by Commoble · Apr 21, 2017 at 10:21 PM

GameObject.tag is a member variable, not a function. Do this instead:

 this.player = GameObject.FindWithTag("Player").transform;

You can read the documentation for FindWithTag() here.

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 NightmaresDev · Apr 22, 2017 at 08:22 AM 0
Share

I wanna Thank you a lot! Because this is currently working perfectly fine!

Thanks!

avatar image
1

Answer by Razputin · Apr 22, 2017 at 01:57 AM

player = GameObject.FindWithTag("Player").transform;

Why do you want your players transform though? Might be easier to just grab the whole thing instead.

Public GameObject player;

player = GameObject.FindWithTag("Player");

Then if you want to call the transform anywhere just call

player.transform;

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 NightmaresDev · Apr 22, 2017 at 08:23 AM 0
Share

Thank you ^^

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I get my Player (which is a square) to move smoothly in Unity 2d? I still need help, even though there are answers. 2 Answers

Error: CS0246 Can't access Player Health Script 1 Answer

hey, is it possible to use a teleport transform script that will teleport you to a certain location on a different scene? 2 Answers

Multiple Cars not working 1 Answer

,I am trying to code a dash in c#. I cant get it, help! 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