运行Unity Test Runner PlayMode时如何修复NullReferenceException?

时间:2019-04-24 19:57:58

标签: unity3d

我正在学习如何使用Unity Test Runner,并且在运行代码时遇到问题。 我试图测试启动协程并遵循某些指令的函数“ LearnThrottle”。

public class LearnPedalTest
{
    // A UnityTest behaves like a coroutine in Play Mode. In Edit Mode you can use
    // `yield return null;` to skip a frame.
    [UnityTest]
    public IEnumerator LearnTest()
    {
        //SETUP THE GAME SCENE YOU WANT TO TEST
        //Store the test scene in a temp variable
        var testScene = UnityEngine.SceneManagement.SceneManager.GetActiveScene();
        //Load the game scene you want to use
        yield return UnityEngine.SceneManagement.SceneManager.LoadSceneAsync("MainMenu", LoadSceneMode.Additive);
        //After it loaded, set the scene to active
            UnityEngine.SceneManagement.SceneManager.SetActiveScene(UnityEngine.SceneManagement.SceneManager.GetSceneByName("MainMenu"));

        yield return new WaitForSeconds(1f);

        //TEST
        var hardware = new MonoBehaviourTest<HardwareConfiguration>().component;
        hardware.LearnThrottle();

        yield return new WaitForSeconds(8f);
        string throttleMin = PlayerPrefs.GetString(HardwareConfiguration.MIN_THROTTLE_KEY, "");

        Assert.AreNotEqual(throttleMin, "");

        //CLEANUP THE GAME SCENE
        //Set active scene back
            UnityEngine.SceneManagement.SceneManager.SetActiveScene(testScene);
        //Cleanup the game scene
        yield return UnityEngine.SceneManagement.SceneManager.UnloadSceneAsync("MainMenu");
    }
}

这是我在游戏场景中运行的脚本,并且可以正常工作。

[SerializeField] private HandleNotification handleNotification;

...

public void LearnThrottle()
{
    if (m_running)
        return;
    m_running = true;
    StartCoroutine(HandleLearning(Pedal.Throttle));
}

...

private IEnumerator HandleLearning(Pedal pedal)
{
    handleNotification.ShowNotification("Desacione todos os pedais...");
    yield return new WaitForSeconds(WAIT_TIME);

    float currentValue = 0;

    switch(pedal)
    {
         ...
    }
}

错误是handleNotification上的NullReferenceException,并且在Unity Inspector中设置。

0 个答案:

没有答案