实例化新对象时音频不播放

时间:2015-12-18 11:52:23

标签: c# unity3d

我在项目中遇到音频问题。有三个桨式游戏对象和一个立方体。多维数据集包含Rigidbody2dBoxCollider2d。还有一个按钮脚本附加到多维数据集,当我们点击按钮多维数据集Kinematic变为false并掉落在游泳池上时,它具有按钮功能。当它与任何桨状立方体发生碰撞时,再次使用新预制件进行实例化。当Cube下降时,声音会播放,立方体会被破坏。当再次点击按钮然后出现错误时,新的立方体就会被实例化。

  

MissingReferenceExceptionAudioSource类型的对象已被销毁,但您仍在尝试访问它。您的脚本应该检查它是否为null或者您不应该销毁该对象。

所有拨片上的脚本:

public class Paddle : MonoBehaviour
{
    [SerializeField]
    private AudioSource source;
    [SerializeField]
    private AudioClip hit;
    [SerializeField]
    private BoxCollider2D collide;
    [SerializeField]
    private GameObject Clone;

    void Awake()
    {
        collide.isTrigger = true;
    }

    void OnTriggerEnter2D(Collider2D target)
    {
        source.PlayOneShot(hit);
        Destroy(target.gameObject);
        Instantiate(Clone, new Vector3(0f, 4.51f, 0f), Quaternion.identity);
    }
}

多维数据集脚本:

public class Cube : MonoBehaviour 
{
    [SerializeField]
    private AudioSource source;
    [SerializeField]
    private Rigidbody2D body;
    [SerializeField]
    private AudioClip play;
    private Button bt;
    private float pos;
    public bool check;

    void Start()
    {
        bt = GameObject.FindGameObjectWithTag("Button").GetComponent<Button>();
        bt.onClick.AddListener(Fire);
        body.isKinematic = true;
    }

    void Update()
    {
        if (check)
        {
            return;
        }

        Vector3 temp = transform.position;
        pos = Camera.main.ScreenToWorldPoint(Input.mousePosition).x;
        temp.x = Mathf.Clamp(pos, -6.25f, 6.25f);
        body.position = temp;
    }

    public void Fire()
    {
        GameObject.FindGameObjectWithTag("Player").GetComponent<Cube>().check = true;
        GameObject.FindGameObjectWithTag("Player").GetComponent<Rigidbody2D>().isKinematic = false;
        source.PlayOneShot(play);
    }
}

多维数据集图片 enter image description here

划桨图片 enter image description here

新出口套餐: https://drive.google.com/file/d/0B1H5fdK2PJAnSXVPdmE5Z3J1SUU

视频问题: https://drive.google.com/file/d/0B1H5fdK2PJAnYzRfVnlQT1FyTlE

1 个答案:

答案 0 :(得分:3)

您的链接包工作正常,每次启动和销毁多维数据集时都会播放声音。

尽管如此,你应该删除destroy上的方法。当非持久性监听器为空时,Unity不会抛出任何错误或警告,有点奇怪,但事实就是这样。您应该手动删除它:

void OnDestroy(){
    bt.onClick.RemoveListener (Fire);
}

但是当我运行它时,你的包不会抛出任何错误。

虽然,我会重新考虑你的方法,而不是将其Fire方法分配给Button事件,我会在按钮上包含一个包含Fire方法的脚本以及AudoiSource和clip。然后在Start上,Cube将自行传递,以便按钮可以访问其Cube和Rigidbody2D组件。

最好是传递包含这些成员的类:

public class CubeArgument{
    public readonly Rigidbody2D rg = null;
    public readonly Cube cube = null;  
    public CubeArgument(Rigidbody2D rg, Cube cube){
        this.rg = rg;
        this.cube = cube;
    }
}

然后这是你的Cube启动方法:

void Start () {
    bt = GameObject.FindGameObjectWithTag ("Button");
    bt.GetComponent<ButtonController> ().Init(new CubeArgument(body, this));
    body.isKinematic = true;
}

ButtonController参考甚至可以是静态的,因为整个级别只有一个。

然后在按钮上有一个ButtonController:

public class ButtonController : MonoBehaviour{

     Cube currentCube = null;
     Rigodbody2D currentRig = null;
     public void Init(CubeArgument ca){
         currentRig = ca.rg;
         currentCube = ca.cube;
     }
     public void Fire(){
         if(currentCube != null){ currentCube.check = true; }
         if(currentRig != null) { currentRig.isKinematic = false; }
     }
}

Fire作为侦听器传递给Button onClick,就是这样。

相关问题