Search Unity

Question Which gameObject should I attach my Chronometer script to ?

Discussion in 'Scripting' started by BloomyFractal, Aug 18, 2021.

  1. BloomyFractal

    BloomyFractal

    Joined:
    May 8, 2021
    Posts:
    16
    Hello, I am trying to implement a simple chronometer feature in order to complete a Tutorial. What I struggle with is manipulation of the TimeText Object. When I attach the Chronometer Script to the Canvas, and then drag the TimeText, I cannot drop it into the public Text variable. I don't understand what should be done in order to show time running down in the scene. I would like help about this issue please. Unity Chronometer Problem.png
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,911
    From your screenshot you have the inspector of the script itself open. You need to open the inspector for the GameObject you attached to script to in the scene (if you attached it to the Canvas object, then that one). From the hierarchy window. Then you can drag objects into the Time Text slot there.
     
    Joe-Censored likes this.
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    With Unity you can pretty much attach scripts to any GameObject. What matters is anything in the script which already assumes it will be attached to a certain GameObject (such as when you use gameObject.GetComponent<>() to get a reference to another component on the same GameObject), and your planned life cycle for the GameObject. Also, usually you want to attach scripts to some logical place, so you can easily find it months later.
     
  4. BloomyFractal

    BloomyFractal

    Joined:
    May 8, 2021
    Posts:
    16
    I attached the Chronometer script to the Canvas, but I cannot drop the TimeText gameObject into the public Time Text variable. I don't understand. Unity Chronometer Problem 2.png
     
  5. BloomyFractal

    BloomyFractal

    Joined:
    May 8, 2021
    Posts:
    16
    SOLVED ! The solution is to use A Text UI instead of a TextMeshPro UI. Now I can drag the TimeText gameObject into the public TimeText variable.