尝试制作音频源时,标记“=”无效

时间:2015-06-28 15:17:59

标签: c# unity3d

拥有通常无效的令牌“=”我通常可以修复。这次我无法绕过什么错误。我是一个新的,年轻的程序员,正在学习,所以这可能是非常明显的,但如果你能回复感谢真的会有所帮助!

这是错误的代码

audio = (AudioSource)AudioClip.AddComponent ("AudioSource");
AudioClip myAudioClip;
myAudioClip = (AudioClip)Resources.Load("SFX/DoNotQuestionTheAdventureVoice");
audio.clip = myAudioClip;

如果您需要,这是完整的代码:

using UnityEngine;
using System.Collections;

public class Button : MonoBehaviour {
    public AudioSource audio;
    public AudioClip buttonSound;

    void Update(){}

    void start ();

    audio = (AudioSource)AudioClip.AddComponent ("AudioSource");
    AudioClip myAudioClip;
    myAudioClip = (AudioClip)Resources.Load("SFX/DoNotQuestionTheAdventureVoice");
    audio.clip = myAudioClip;


    void OnGUI () {
        if (GUI.Button (new Rect (300, 300, 400, 50), "Do not question the adventure voice!")) {
                audio.Play ();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

也许你的代码格式不正确,但基于此,你的代码在你的类声明中,但看起来应该在start方法中。

重新格式化并修复了以下代码。

using UnityEngine;
using System.Collections;

public class Button : MonoBehaviour
{
    public AudioSource audio;
    public AudioClip buttonSound;

    void Update()
    {
    }

    // Changed from start(); which is not valid
    // Also, this may need to be "Start" (note the difference in casing)
    void start()
    {
       audio = (AudioSource)AudioClip.AddComponent ("AudioSource");
       AudioClip myAudioClip;
       myAudioClip = (AudioClip)Resources.Load("SFX/DoNotQuestionTheAdventureVoice");
       audio.clip = myAudioClip;
    }

    void OnGUI ()
    {
        if (GUI.Button (new Rect (300, 300, 400, 50), "Do not question the adventure voice!")) {
        audio.Play ();
    }
}