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 How to make button press effect work

Discussion in 'Audio & Video' started by itsdandd, May 9, 2023.

  1. itsdandd

    itsdandd

    Joined:
    Jan 6, 2020
    Posts:
    44
    I have a button that I added a sound effect using the onclick:
    upload_2023-5-9_15-41-40.png
    It works well but it doesnt work at all when transferring scenes as the sound doesnt play at all.

    Is there any way to fix this?
     
  2. itsdandd

    itsdandd

    Joined:
    Jan 6, 2020
    Posts:
    44
    I tried this script and it works but it saves the wrong gameobject as the new one that's made when the scene loads is the one that plays the sound.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class dontdestoy : MonoBehaviour
    6. {
    7.     void Awake()
    8.     {
    9.         GameObject[] objs = GameObject.FindGameObjectsWithTag("Music");
    10.  
    11.         if (objs.Length > 1)
    12.         {
    13.             Destroy(this.gameObject);
    14.         }
    15.         DontDestroyOnLoad(this.gameObject);
    16.     }
    17. }
    18.  
    I feel that there is a way around but I am not skilled enough to know it or find it.