Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

easy Question getComponent [SOLVED]

Discussion in 'Scripting' started by Uselchen, Jan 27, 2015.

  1. Uselchen

    Uselchen

    Joined:
    Jul 20, 2014
    Posts:
    11
    Simple question, how do i tell the variable "player" that it has to look for the Gameobject with the Tag Player.

    The script is for a Homing missle which is instanciated as a prefab shot vom a Turret.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Homing : MonoBehaviour {
    5.  
    6.     public float speed;
    7.     private Transform player;
    8.  
    9.  
    10.     void Start() // how do i say that player is the GameObject with the tag Player ???
    11.     {
    12.         player = gameObject.GetComponent<transform>GameObject.FindGameObjectWithTag("Player");             //GameObject.FindGameObjectWithTag ("Player");  
    13. //  <-- this is not corret right?^^
    14.     }
    15.  
    16.  
    17.     void FixedUpdate ()
    18.     {
    19.         float z = Mathf.Atan2 ((player.transform.position.y - transform.position.y), (player.transform.position.x - transform.position.x)) * Mathf.Rad2Deg - 90;
    20.         transform.eulerAngles = new Vector3 (0, 0, z);
    21.         rigidbody2D.AddForce (gameObject.transform.up * speed);
    22.     }
    23.  
    24.     void OnTriggerEnter2D (Collider2D coll)
    25.     {
    26.         if (coll.gameObject.tag == "Respawn") {
    27.          
    28.          
    29.             Destroy(gameObject); // , 0.3f   für ani delay
    30.          
    31.         }
    32.  
    33.     }
    34.  
    35.  
    36. }
     
  2. jaasso

    jaasso

    Joined:
    Jun 19, 2013
    Posts:
    64
    do you want your player variable to be a transform or a GameObject?

    your player variable is a transform so

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

    if you make player a GameObject leave .transform out
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
  4. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    Code (CSharp):
    1.     void Start()
    2.     {
    3.         GameObject goPlayer = GameObject.FindGameObjectWithTag("Player");
    4.  
    5.         if (goPlayer != null) {
    6.                player = goPlayer.transform;
    7.         }
    8.     }
     
  5. Ramsdal

    Ramsdal

    Joined:
    Oct 18, 2013
    Posts:
    251
    Change
    player = gameObject.GetComponent<transform>GameObject.FindGameObjectWithTag("Player");

    To
    player = GameObject.FindGameObjectWithTag ("Player").transform;
     
    hhjavadi likes this.
  6. Ramsdal

    Ramsdal

    Joined:
    Oct 18, 2013
    Posts:
    251
    Hehe.

    Thats what comes from opening windows and not replying instantly - well what Sbizz said, cheching for null would be best ;)
     
  7. Uselchen

    Uselchen

    Joined:
    Jul 20, 2014
    Posts:
    11
    thats exactly what i was looking for :)

    THX !