Unity + FMOD健康玩家变量到getParameter不起作用

时间:2017-01-06 11:46:09

标签: c# unity3d fmod

请你帮我解决这个问题,我将FMOD添加到UNITY并希望在播放器受到损坏时更改我的音乐,从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)
    }

}

感谢您提前

1 个答案:

答案 0 :(得分:0)

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

您尝试在MenuMusic()LevelMusic()函数中初始化它们,但无法保证在实际使用它们的Update()函数之前调用这些函数。

musicEv = FMODUnity.RuntimeManager.CreateInstance(menuMusic);MenuMusic()个功能移除LevelMusic()并将其移至Start()Awake()功能,以初始化musicEv

之后,您可以通过调用musicPar来初始化musicEv.getParameter("HEALTH", out 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);
    }
}