您继承自MonoBehaviour
which 继承自Behaviour
,然后从Component
which 中声明了一些变量,例如:
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 开始就被弃用了。您链接的文档尚未更新。有关更多信息,请参阅这篇文章。
您所要做的就是将您的音频变量重命名为其他名称,一切都应该可以正常工作。
public AudioClip impact;
AudioSource myAudio;
void Start()
{
myAudio = GetComponent<AudioSource>();
}
public void play_sound()
{
myAudio.PlayOneShot(impact, 0.7F);
}