Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

transform to text?

Discussion in 'Scripting' started by Resilo, Dec 28, 2016.

  1. Resilo

    Resilo

    Joined:
    Dec 8, 2016
    Posts:
    139
    So im working on a system so that the name of the gameobject the transform target is attached to will be displayed as text in the ui



    this is what i have and im not sure why it is not displaying the name at this point


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class PlayerAttack : BattleControl {
    6.  
    7.     public float timeBetweenattacks;
    8.     public int attackDamage;
    9.     public float attackrange;
    10.     public Transform select;
    11.     public Animator attackanimaton;
    12.     public Text targetselect;
    13.     // Use this for initialization
    14.  
    15.  
    16.     IEnumerator attack()
    17.     {
    18.         attackanimaton.SetBool("attacker", true);
    19.         health -= attackDamage;
    20.         yield return new WaitForSeconds(0.30f);
    21.         attackanimaton.SetBool("attacker", false);
    22.      
    23.     }
    24.  
    25.  
    26.     void attacking()
    27.     {
    28.         RaycastHit hit;
    29.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    30.         if (Input.GetMouseButtonDown(0))
    31.         {
    32.             attackanimaton.SetBool("isAttacking", true);
    33.  
    34.             {
    35.  
    36.                 if (Physics.Raycast(ray, out hit))
    37.                 {
    38.  
    39.                     if (hit.transform.CompareTag("Battler"))
    40.                     {
    41.                         target = hit.transform;
    42.                         Debug.Log("1" + target);
    43.                         StartCoroutine(attack());
    44.                     }
    45.  
    46.  
    47.                     else if (!hit.transform.CompareTag("Battler"))
    48.                     {
    49.                         target = null;
    50.                     }
    51.  
    52.                 }
    53.             }
    54.         }
    55.     }
    56.  
    57.  
    58.  
    59.  
    60.  
    61.  
    62.     void Start()
    63.     {
    64.         targetselect.text = (target.gameObject.name);
    65.         health = GetComponent<Battler>().health;
    66.     }
    67.  
    68.     // Update is called once per frame
    69.     void Update()
    70.     {
    71.         attacking();
    72.     }
    73. }
    74.  
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    you probably want the transform.position, transform contains all the data a thing can have
     
  3. Resilo

    Resilo

    Joined:
    Dec 8, 2016
    Posts:
    139
    says cannot convert type vector to string
     
  4. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    You need to call .ToString() in that case, but first, please tell me, did you even touch C# before attempting unity?
     
  5. Resilo

    Resilo

    Joined:
    Dec 8, 2016
    Posts:
    139
    Done a little bit of C++ in the past so at this point im self teaching/learning thanks for the help so far.

    as an example i know a vector is not a string and holds no information related to a string so i was a little confused at your suggestion in the first place i did attempt to use tostring before i just had the wrong syntax for target


    the best solution was this


    Code (CSharp):
    1.     void Update()
    2.     {
    3.         attacking();
    4.  
    5.         if (target.transform.CompareTag("Battler"))
    6.         {
    7.             targetselect.text = target.transform.root.gameObject.name;
    8.         }
    9.        
    10.     }
     
    Last edited: Dec 28, 2016