English 中文(简体)
为什么这种音响在团结地重塑现场时没有发挥作用?
原标题:Why does this audio clip not play while reseting the scene in unity?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class LogicScript : MonoBehaviour
{
   public int score;
    public Text scoreText;
    public GameObject GameOverScreen;



    [ContextMenu ("add score")]
    public void addScore(int scoreToAdd)
    { 
        score += scoreToAdd;
        scoreText.text = score.ToString() +" Majors";
    }
    public void restartGame()
    {
        
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
        CanI();
         
    }

    public void GameOver()
    {
        GameOverScreen.SetActive(true);
    }

    public AudioSource src;
    public AudioClip sfx, sfx2, sfx3, sfx4; 

    public void weMajor()
    {
        src.clip = sfx;
        src.Play();
    }

    public void End()
    {
        src.clip = sfx2;
        src.Play();
    }
    public void CanI()
    {
        src.clip = sfx3;
        src.Play();
    }

    public void Intro()
    {
        src.clip = sfx4;
        src.Play();
    }

}

Im calling the restartGame() function using a button, but the CanI() function never plays any audio. I know both the button and the CanI() function work independently but for some reason not when combining them like this. In my mind the scene should reset, the audio should play and then the computer exits the function. Why dosent this happen, what am i supposed to do?

问题回答

<代码>CanI(功能在装载后产生 因此,不会说。

为此:

public void restartGame()
{
    CanI();
    SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}

with SceneManager.LoadScene(SceneManager.GetActiveScene().name); unity destroy your LogicScript and create new one. as you didn t add any Awake or Start method to call when new object instantiated the audio clip won t play. i suggest to seprate your Audio and your logic into two different scripts. create an singleton script called AudioManager and add DontDestroyOnLoad:

void Awake()
{
    DontDestroyOnLoad(gameObject);
}

在您的首场景中,这种文字从未销毁。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签