英文:
How to Resolve ArgumentNullException in Unity When Playing Sound in a Function?
问题
I have a Unity problem where I am trying to play sound when addScore() function starts using dingSFX.Play();, but instead I get the following error:
ArgumentNullException: Value cannot be null.
我在Unity中遇到问题,我尝试在addScore()函数开始时播放声音,使用dingSFX.Play();,但我却收到以下错误消息:ArgumentNullException: 值不能为null。
I am using Audio Source component that I added into my Inspector tab where also my script(LogicScript.cs) is. I also added the AudioClip into Audio Source.
我正在使用添加到我的Inspector选项卡中的Audio Source组件,我的脚本(LogicScript.cs)也在那里。我还将AudioClip添加到了Audio Source中。
LogicScript.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class LogicScript : MonoBehaviour
{
public int playerScore;
public Text scoreText;
public GameObject gameOverScreen;
public AudioSource dingSFX;
[ContextMenu("Increase Score")]
public void addScore(int scoreToAdd)
{
playerScore = playerScore + scoreToAdd;
scoreText.text = playerScore.ToString();
dingSFX.Play(); // this does not work
}
public void restartGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
public void gameOver()
{
gameOverScreen.SetActive(true);
}
}
LogicScript.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class LogicScript : MonoBehaviour
{
public int playerScore;
public Text scoreText;
public GameObject gameOverScreen;
public AudioSource dingSFX;
[ContextMenu("Increase Score")]
public void addScore(int scoreToAdd)
{
playerScore = playerScore + scoreToAdd;
scoreText.text = playerScore.ToString();
dingSFX.Play(); // 这部分不起作用
}
public void restartGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
public void gameOver()
{
gameOverScreen.SetActive(true);
}
}
LogicScript.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class LogicScript : MonoBehaviour
{
public int playerScore;
public Text scoreText;
public GameObject gameOverScreen;
public AudioSource dingSFX;
[ContextMenu("Increase Score")]
public void addScore(int scoreToAdd)
{
playerScore = playerScore + scoreToAdd;
scoreText.text = playerScore.ToString();
dingSFX.Play(); // これは機能しません
}
public void restartGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
public void gameOver()
{
gameOverScreen.SetActive(true);
}
}
LogicScript.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class LogicScript : MonoBehaviour
{
public int playerScore;
public Text scoreText;
public GameObject gameOverScreen;
public AudioSource dingSFX;
[ContextMenu("Increase Score")]
public void addScore(int scoreToAdd)
{
playerScore = playerScore + scoreToAdd;
scoreText.text = playerScore.ToString();
dingSFX.Play(); // これは機能しません
}
public void restartGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
public void gameOver()
{
gameOverScreen.SetActive(true);
}
}
LogicScript.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class LogicScript : MonoBehaviour
{
public int playerScore;
public Text scoreText;
public GameObject gameOverScreen;
public AudioSource dingSFX;
[ContextMenu("Increase Score")]
public void addScore(int scoreToAdd)
{
playerScore = playerScore + scoreToAdd;
scoreText.text = playerScore.ToString();
dingSFX.Play(); // これは機能しません
}
public void restartGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
public void gameOver()
{
gameOverScreen.SetActive(true);
}
}
英文:
I have a Unity problem where I am trying to play sound when addScore() function starts using dingSFX.Play();, but instead I get the following error:
ArgumentNullException: Value cannot be null.
I am using Audio Source component that I added into my Inspector tab where also my script(LogicScript.cs) is. I also added the AudioClip into Audio Source.
LogicScript.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class LogicScript : MonoBehaviour
{
public int playerScore;
public Text scoreText;
public GameObject gameOverScreen;
public AudioSource dingSFX;
[ContextMenu("Increase Score")]
public void addScore(int scoreToAdd)
{
playerScore = playerScore + scoreToAdd;
scoreText.text = playerScore.ToString();
dingSFX.Play(); // this does not work
}
public void restartGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
public void gameOver()
{
gameOverScreen.SetActive(true);
}
}
答案1
得分: 0
将音频源分配给音频源,而不是将音频剪辑分配给音频源。
创建一个空的游戏对象,随便取名,为其添加一个音频源组件,将剪辑拖放到音频源的剪辑字段中,然后将包含音频源的游戏对象拖放到脚本中的dingSFX上。
英文:
You have to assign an audio source to an audio source, not an audio clip to an audio source.
Create an empty gameobject, call it whatever you want, add an audio source component to it, drag the clip into the clip field of the audio source, and drag the audio source containing gameobject to the dingSFX on your script.
答案2
得分: 0
The script itself has no error. The only reason why this doesn't work is because you didn't assign your audio source to your script in the inspector. Which means that your script couldn't find an audio source.
Check out this video for an example.
Since you've mentioned that the script and the audio source are both on the same gameObject, you can try:
void Start()
{
dingSFX = GetComponent<AudioSource>();
}
This way you get reference via script so you won't forget to assign it in the inspector. Hope this helps!
英文:
Like what others mentioned, the script itself has no error. The only reason why this doesn't work is because you didn't assign your audio source to your script in the inspector. Which means that your script couldn't find an audio source.
Check out this video for an example.
Since you've mentioned that the script and the audio source are both on the same gameObject, you can try:
void Start()
{
dingSFX = GetComponent<AudioSource>();
}
This way you get reference via script so you won't forget to assign it in the inspector. Hope this helps!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论