Unity3D AudioSource没有附加到Object?

时间:2017-04-30 14:57:12

标签: c# unity3d audio-source

嘿。我有一个音频源组件附加到我的对象。我还从here复制了脚本并将其附加到我的对象上。

 using UnityEngine;
 using System.Collections;

 [RequireComponent(typeof(AudioSource))]
 public class ExampleClass : MonoBehaviour
 {
     public AudioClip impact;
     AudioSource audio;

     void Start()
     {
         audio = GetComponent<AudioSource>();
     }

     public void play_sound()
     {
         audio.PlayOneShot(impact, 0.7F);
     }
 }

但是当我尝试在我的脚本中使用音频时,我会将其剪切掉 enter image description here

问题是什么?

1 个答案:

答案 0 :(得分:1)

您继承自MonoBehaviour,继承自Behaviour,然后继承自Component,其中包含一些变量,例如:

public Component animation { get; }
public Component audio { get; }
public Component camera { get; }
public Component collider { get; }
public Component collider2D { get; }
public Component constantForce { get; }
public Component guiElement { get; }
public Component guiText { get; }
public Component guiTexture { get; }
public Component hingeJoint { get; }
public Component light { get; }
public Component networkView { get; }
public Component particleEmitter { get; 
public Component particleSystem { get; }
public Component renderer { get; }
public Component rigidbody { get; }
public Component rigidbody2D { get; }

这些都已弃用,不再允许在最新版本的Unity中使用。我相信它已从Unity 5向上弃用。您链接的文档尚未更新。有关详细信息,请参阅this发布。

您所要做的就是将音频变量重命名为其他内容,一切都应该正常。

public AudioClip impact;
AudioSource myAudio;

void Start()
{
    myAudio = GetComponent<AudioSource>();
}

public void play_sound()
{
    myAudio.PlayOneShot(impact, 0.7F);
}