Search Unity

Question my code doesnt seem to let me put in a text mesh pro into the text mesh pro slot. heres my code vvv

Discussion in 'Scripting' started by loony_ninja, May 18, 2023.

  1. loony_ninja

    loony_ninja

    Joined:
    May 10, 2023
    Posts:
    2
    can i have help


    Code (CSharp):
    1.  
    2.  using System.Collections;
    3.  using System.Collections.Generic;
    4.  using UnityEngine;
    5.  using UnityEngine.UI;
    6.  using TMPro;
    7.  
    8.  public class PlayerXp : MonoBehaviour
    9.  {
    10.      public int level;
    11.      public float currentXp;
    12.      public float requiredXp;
    13.  
    14.      private float lerpTimer;
    15.      private float delayTimer;
    16.      [Header("UI")]
    17.      public Image frontXPBar;
    18.      public Image backXPBar;
    19.      public TextMeshPro LevelText;
    20.      [Header("Multiplier")]
    21.      [Range(1f,300f)]
    22.      public float additionMultiplier = 300;
    23.      [Range(2f,4f)]
    24.      public float powerMultiplier = 2;
    25.      [Range(7f,14f)]
    26.      public float divisionMultiplier = 7;
    27.      // Start is called before the first frame update
    28.      void Start()
    29.      {
    30.          frontXPBar.fillAmount = currentXp / requiredXp;
    31.          backXPBar.fillAmount = currentXp / requiredXp;
    32.          requiredXp = CalculateRequiredXp();
    33.      }
    34.  
    35.      // Update is called once per frame
    36.      void Update()
    37.      {
    38.          UpdateXpUI();
    39.          if(Input.GetKeyDown(KeyCode.X))
    40.              GainExperienceFlatRate(20);
    41.          if(currentXp > requiredXp)
    42.              LevelUp();
    43.      }
    44.      public void UpdateXpUI()
    45.      {
    46.          float xpFraction = currentXp / requiredXp;
    47.          float FXP = frontXPBar.fillAmount;
    48.          if (FXP < xpFraction)
    49.          {
    50.              delayTimer += Time.deltaTime;
    51.              backXPBar.fillAmount = xpFraction;
    52.              if(delayTimer > 3)
    53.              {
    54.                  lerpTimer += Time.deltaTime;
    55.                  float percentComplete = lerpTimer / 4;
    56.                  frontXPBar.fillAmount = Mathf.Lerp(FXP, backXPBar.fillAmount, percentComplete);
    57.              }
    58.          }
    59.      }
    60.      public void GainExperienceFlatRate(float xpGained)
    61.      {
    62.          currentXp += xpGained;
    63.          lerpTimer = 0f;
    64.          delayTimer = 0f;
    65.      }
    66.      public void GainExperienceScalable(float xpGained, int passedLevel)
    67.      {
    68.          if(passedLevel < level)
    69.          {
    70.              float multiplier = 1+ (level - passedLevel) * 0.1f;
    71.              currentXp += xpGained * multiplier;
    72.          }
    73.          else
    74.          {
    75.              currentXp += xpGained;
    76.          }
    77.          lerpTimer = 0f;
    78.          delayTimer = 0f;
    79.      }
    80.      public void LevelUp()
    81.      {
    82.          level++;
    83.          frontXPBar.fillAmount = 0f;
    84.          backXPBar.fillAmount = 0f;
    85.          currentXp = Mathf.RoundToInt(currentXp - requiredXp);
    86.          /// Skillpoints and other modifications VVV
    87.          GetComponent<playerHealth>().IncreaseHealth(level);
    88.          /// not mods
    89.          requiredXp = CalculateRequiredXp();
    90.          LevelText.text = "" + level;
    91.        
    92.  
    93.      }
    94.  
    95.      private int CalculateRequiredXp()
    96.      {
    97.          int solveForRequiredXp = 0;
    98.          for(int levelCycle = 1; levelCycle <= level; levelCycle++)
    99.          {
    100.              solveForRequiredXp += (int)Mathf.Floor(levelCycle + additionMultiplier * Mathf.Pow(powerMultiplier, levelCycle / divisionMultiplier));
    101.          }
    102.          return solveForRequiredXp / 4;
    103.      }
    104.    
    105.  }
    106.  
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,914
    You are likely confusing TextMeshPro (which is the 3D mesh version) with TextMeshProUGUI (the UI version).

    I recommend always just using TMP_Text, since it will work for both types.
     
    loony_ninja, Kurt-Dekker and mopthrow like this.
  3. loony_ninja

    loony_ninja

    Joined:
    May 10, 2023
    Posts:
    2
    Hey it worked thanks
     
    Kurt-Dekker likes this.