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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question I can't drag text into my script

Discussion in 'Scripting' started by Self_enes, Sep 11, 2023.

  1. Self_enes

    Self_enes

    Joined:
    Aug 29, 2023
    Posts:
    7
    I can't drag text into my script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class AudSys : MonoBehaviour
    7. {
    8.     public Text volumeAmount;
    9.  
    10.     public void SetAudio(float value)
    11.     {
    12.         AudioListener.volume = value;
    13.         volumeAmount.text = ((int)value * 100).ToString();
    14.     }
    15. }
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    So.. what's the problem? You should be able to see the volumeAmount Text in the inspector.
    Does the object you attempt to drag there have a Text component?
     
  3. Self_enes

    Self_enes

    Joined:
    Aug 29, 2023
    Posts:
    7
    yes ?
     
  4. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    Is it the correct Text type? Do you have any errors in your console? What happens when you drag it there?

    This would be easier if you provided a bit more information.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    37,142
    There's about 7 or 8 different things that one might call "Text" in Unity.

    If it isn't the
    UnityEngine.UI.Text
    component (what your script is asking for above) it won't slot in.

    TMPro texts won't slot in, UIElement texts won't slot in, and the old legacy TextMesh won't slot in either.
     
  6. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    Instead of dragging you can click on the volumeAmount field in the inspector and that will open a box with the available choices.
     
  7. CharlieCook

    CharlieCook

    Joined:
    May 10, 2019
    Posts:
    13
    TMPro maybe???


    Code (CSharp):
    1. using TMPro;
    2.  
    3. public class GameManager : MonoBehaviour
    4. {
    5.       public TextMeshProUGUI volume;
    6.  
    7. }
     
  8. NicBischoff

    NicBischoff

    Joined:
    Mar 19, 2014
    Posts:
    204

    AudioListener Is defined. Your script has errors and won’t compile so it’s not working.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class AudSys : MonoBehaviour
    7. {
    8.     public Text volumeAmount;
    9.  
    10.     // Reference to the AudioListener component assigned in the Inspector
    11.     public AudioListener audioListener;
    12.  
    13.     public void SetAudio(float value)
    14.     {
    15.         if (audioListener != null)
    16.         {
    17.             audioListener.volume = value;
    18.             volumeAmount.text = ((int)(value * 100)).ToString();
    19.         }
    20.         else
    21.         {
    22.             Debug.LogWarning("AudioListener is not assigned.");
    23.         }
    24.     }
    25. }
     
  9. NicBischoff

    NicBischoff

    Joined:
    Mar 19, 2014
    Posts:
    204
    Just a note to look into using log to manage your audio volume. It’s best not to linearly scale audio volume as db doesn’t work like that.