Search Unity

Question How to change ui text child of canvas screen space overlay ?

Discussion in 'Scripting' started by haimmoshe, Jan 17, 2022.

  1. haimmoshe

    haimmoshe

    Joined:
    Jun 3, 2017
    Posts:
    237
    I have this script attached to a cube :

    I want to be able to change the ui text textName position.
    The problem is in the Update I'm setting the textName position because I want the text to be above the object but then I can's change the textName position in other places in the code like in the Start.

    Before all that the script was attached to a child object of the Cube so I could change the child position and the text was moving with the child object but because I also want to use OnTriggerEvent I moved the script to the Cube it self the parent and now the text is in the middle of the cube and not above it.

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class Rotations : MonoBehaviour
    8. {
    9.     public Text textName;
    10.     public float rotationSpeed;
    11.     public bool rotateText = false;
    12.     public string textToDisplay;
    13.     public float displayCurrentRotationSpeed;
    14.  
    15.     public bool easeInOut = false;
    16.     public float rotationDuration;
    17.     public float startAngle;
    18.     public float stopAngle;
    19.     public AnimationCurve curve;
    20.  
    21.     private float currentRotationTime;
    22.  
    23.     private void Start()
    24.     {
    25.         textName.text = textToDisplay;
    26.     }
    27.  
    28.     private void Update()
    29.     {
    30.         Vector3 namePos = Camera.main.WorldToScreenPoint(this.transform.position);
    31.         textName.transform.position = namePos;
    32.  
    33.         if(rotateText)
    34.         {
    35.             displayCurrentRotationSpeed = rotationSpeed * currentRotationTime;
    36.  
    37.             if (easeInOut)
    38.             {
    39.  
    40.                 currentRotationTime += Time.deltaTime;
    41.                 var progress = curve.Evaluate(currentRotationTime / rotationDuration);
    42.                 var currentAngle = startAngle + (stopAngle - startAngle) * progress;
    43.                 textName.transform.rotation = Quaternion.Euler(0f, currentAngle, 0f);
    44.             }
    45.             else
    46.             {
    47.                 if (displayCurrentRotationSpeed < 3f)
    48.                 {
    49.                     currentRotationTime += Time.deltaTime;
    50.                     textName.transform.Rotate(0, displayCurrentRotationSpeed, 0);
    51.                 }
    52.                 else
    53.                 {
    54.                     textName.transform.Rotate(0, displayCurrentRotationSpeed, 0);
    55.                 }
    56.             }
    57.         }
    58.     }
    59. }
    60.  
     
    Last edited: Jan 17, 2022
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,692
    That is the problem, so fix it.

    Centralize control of the things position into its own script and then make calls that do what you want.

    For instance if the object was a sword, it might have two places:

    - in the scabbard while I'm walking
    - in my hand while I'm fighting

    But when I'm fighting I am walking... so you have to tie it into something that observes (or is told) the proper state to go into.

    - go into walking mode (in Start() and at end of combats)
    - go into fighting mode (when fighting mode starts)