Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Unity中的音频源异常_C#_Unity3d - Fatal编程技术网

C# Unity中的音频源异常

C# Unity中的音频源异常,c#,unity3d,C#,Unity3d,因此,我想在发生碰撞触发时播放音频源,但unity会引发此异常: ArgumentNullException:值不能为null。参数名称:源 UnityEngine.AudioSource.Play()(位于 :0)PlanetMovement.OnTriggerEnter2D (UnityEngine.CollizedR2D碰撞)(在 资产/脚本/PlanetMovement.cs:47) 以下是脚本: using System.Collections; using System.Collec

因此,我想在发生碰撞触发时播放音频源,但unity会引发此异常:

ArgumentNullException:值不能为null。参数名称:源 UnityEngine.AudioSource.Play()(位于 :0)PlanetMovement.OnTriggerEnter2D (UnityEngine.CollizedR2D碰撞)(在 资产/脚本/PlanetMovement.cs:47)

以下是脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class PlanetMovement : MonoBehaviour
{
    bool moveAllowed;
    Collider2D col;
    public float moveSpeed;
    public GameObject restartPanel;
    private AudioSource source;

    // Start is called before the first frame update
    void Start()
    {
        source = GetComponent<AudioSource>();
        col = GetComponent<Collider2D>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.touchCount > 0)
        {
            Touch touch = Input.GetTouch(0);
            Vector2 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
            transform.position = Vector2.MoveTowards(transform.position, touchPosition, moveSpeed * Time.deltaTime);

        }
    }
    public void GameOver()
    {
        Invoke("Delay", 1.5f);
    }

    public void Delay()
    {
        restartPanel.SetActive(true);
        Time.timeScale = 0;
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "Asteroid")
        {
            source.Play();
            GameOver();
            Debug.Log("Hit");
        }
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.SceneManagement;
公共类飞机运动:单一行为
{
允许布尔移动;
碰撞柱;
公共交通速度;
公共游戏对象重启面板;
私人音频源;
//在第一帧更新之前调用Start
void Start()
{
source=GetComponent();
col=GetComponent();
}
//每帧调用一次更新
无效更新()
{
如果(Input.touchCount>0)
{
Touch-Touch=Input.GetTouch(0);
Vector2 touchPosition=Camera.main.Screen到世界点(touch.position);
transform.position=Vector2.moveToward(transform.position,touchPosition,moveSpeed*Time.deltaTime);
}
}
公众假期
{
调用(“延迟”,1.5f);
}
公共无效延迟()
{
restartPanel.SetActive(true);
Time.timeScale=0;
}
私有无效OnTiggerEnter2D(已碰撞R2D碰撞)
{
如果(collision.tag==“小行星”)
{
source.Play();
GameOver();
Log(“Hit”);
}
}
}
碰撞触发器在没有音频源的情况下工作正常。我不知道发生了什么事。小行星是一个动态生成的预制体,如果这是有用的信息


谢谢。

我的错。我没有在这个星球上创建一个AudioSource组件,而是创建了一个新的AudioSource游戏对象,然后将它作为这个星球的一个孩子。已经修好了


抱歉。

我已经创建了音频源,在编辑器中将音频剪辑附加到音频源,并将音频源附加到星球游戏对象。错误非常简单。第47行,即
source.Play()有一个空引用,唯一的引用是
source
。这意味着此脚本所在的对象与您的
AudioSource
不同。这意味着
Start
-
source=GetComponent()中的行失败了。是的,我发布了一个答案,对不起,这是我的第一个项目。非常感谢你的回答。关于如何处理这个问题,有一个很大的线索。任何关于空引用的帖子都可以在这里回答。