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

XP Bar Slider Play Sound or SetActive GameObject when is 100% (Lvl UP)

Discussion in 'Scripting' started by blackbrosg, Jan 14, 2020.

  1. blackbrosg

    blackbrosg

    Joined:
    Apr 9, 2018
    Posts:
    20
    hey guys i have this xp bar and i need a script what play any sound if experience is 100% (LvlUP) or set active a gameobject, anybody help me?

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. public partial class UIExperienceBar : MonoBehaviour {
    4.     public GameObject panel;
    5.     public Slider slider;
    6.     public Text statusText;
    7.     void Update() {
    8.         var player = Utils.ClientLocalPlayer();
    9.         panel.SetActive(player != null); // hide while not in the game world
    10.         if (!player) return;
    11.         slider.value =  Mathf.MoveTowards(slider.value, player.ExperiencePercent(), 10f * Time.deltaTime);
    12.         statusText.text = "Lvl." + player.level + " (" + (player.ExperiencePercent() * 100).ToString("F0") + "%)";
    13.         // addon system hooks
    14.         Utils.InvokeMany(typeof(UIExperienceBar), this, "Update_");
    15.     }
    16. }
    17.  
     
  2. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    Code (csharp):
    1. if(slider.value >= 1)
    2. {
    3.     someAudioSource.Play();
    4. }
    Is that what you mean?