Unity + FMOD health player 变量到 getParameter 不起作用

Unity + FMOD health player variable to getParameter doesn't work

你能帮我解决这个问题吗,我正在将 FMOD 添加到 UNITY 并想在 Player 受到损坏时更改我的音乐,从 FMOD 方面是可以的,但在 Unity 中它给我一个错误:NullReferenceException: 对象引用未设置到对象的实例 MusicControl.Update ()(在 Assets/MusicControl.cs:

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using FMOD.Studio;

public class MusicControl : MonoBehaviour {


    [FMODUnity.EventRef]
    public string explosion = "event:/EXPLOSION";
    [FMODUnity.EventRef]
    public string shoot = "event:/SHOOT SOUND";
    [FMODUnity.EventRef]
    public string menuMusic = "event:/MENU MUSIC";

    int val;

    public FMOD.Studio.EventInstance musicEv;
    public FMOD.Studio.ParameterInstance musicPar;

    void Start()
    {
    }

    //music for menu, I'm call this function when my stage starts(menu game)
    public void MenuMusic()
    {
        musicEv = FMODUnity.RuntimeManager.CreateInstance(menuMusic);
        musicEv.start();
    }

    //music for level 1, I'm call this function when my stage starts(level game)
    public void LevelMusic() 
    {
        musicEv = FMODUnity.RuntimeManager.CreateInstance(menuMusic);
        musicEv.setParameterValue("FIGHT MUSIC", 100f);
        musicEv.getParameter("HEALTH", out musicPar);
        musicPar.setValue(100); 

        musicEv.start();
    }

    //I'm call this function when stages is close up
    public void StopMusic()
    {
        musicEv.stop(FMOD.Studio.STOP_MODE.IMMEDIATE);
    }

    // I'm take current Health from Player script
    void Update()

        val = GameObject.Find("Player").GetComponent<Player>().stats.curHealth;

        musicPar.setValue(val); //Unity gives me an error - NullReferenceException: Object reference not set to an instance of an object MusicControl.Update () (at Assets/MusicControl.cs:147)
    }

}

感谢提前

musicEvmusicPar 变量已声明 Update() 函数中使用之前从未初始化。

您试图在 MenuMusic()LevelMusic() 函数中初始化它们,但不能保证这些函数会在您实际使用它们的 Update() 函数之前被调用。

MenuMusic()LevelMusic() 函数中删除 musicEv = FMODUnity.RuntimeManager.CreateInstance(menuMusic); 并将其移至 Start()Awake() 函数以初始化 musicEv.

之后,您可以通过调用 musicEv.getParameter("HEALTH", out musicPar); 来初始化 musicPar

此外,不要在更新函数中执行 val = GameObject.Find("Player")。在 Start()Awake() 函数中执行一次,然后将其保存到全局变量中。事实上,只缓存附加到它的 Player 脚本会很好。

public class MusicControl : MonoBehaviour {


    [FMODUnity.EventRef]
    public string explosion = "event:/EXPLOSION";
    [FMODUnity.EventRef]
    public string shoot = "event:/SHOOT SOUND";
    [FMODUnity.EventRef]
    public string menuMusic = "event:/MENU MUSIC";

    int val;

    public FMOD.Studio.EventInstance musicEv;
    public FMOD.Studio.ParameterInstance musicPar;

    private Player player;

    void Awake()
    {
        //Initialize musicEv
        musicEv = FMODUnity.RuntimeManager.CreateInstance(menuMusic);
        //Initialize musicPar(done with the out keyword)
        musicEv.getParameter("HEALTH", out musicPar);

        //Initialize player
        player = GameObject.Find("Player").GetComponent<Player>();
    }

    //music for menu, I'm call this function when my stage starts(menu game)
    public void MenuMusic()
    {
        musicEv.start();
    }

    //music for level 1, I'm call this function when my stage starts(level game)
    public void LevelMusic() 
    {
        musicEv.setParameterValue("FIGHT MUSIC", 100f);
        musicEv.getParameter("HEALTH", out musicPar);
        musicPar.setValue(100); 

        musicEv.start();
    }

    //I'm call this function when stages is close up
    public void StopMusic()
    {
        musicEv.stop(FMOD.Studio.STOP_MODE.IMMEDIATE);
    }

    // I'm take current Health from Player script
    void Update()
    {
        val = player.stats.curHealth;
        musicPar.setValue(val);
    }
}