C# 创造;“碎砖机”;游戏开始一个关卡后,我甚至在球启动之前就得到了音效

C# 创造;“碎砖机”;游戏开始一个关卡后,我甚至在球启动之前就得到了音效,c#,audio,unity3d,C#,Audio,Unity3d,我仍然是一个非常了解脚本和与之相关的词汇的新手。我现在正在网上学习一门课程,我试图勇敢地从视频中走出来,用不同的声音在不同的砖块上制作自己的脚本 我希望球在从桨上反弹时发出某种声音(“bip”)。然而,当我开始一个关卡时,在球还没发射之前,我就听到了“bip”的声音。在教程视频中,他加入了与球相关的声音,因此在更改了一些代码后没有这个问题。我的仍然存在。我已经有了我的球、砖块和桨的脚本。在我的球脚本中,我有一个public bool hasStarted=false在我启动函数之前。我不想在每个

我仍然是一个非常了解脚本和与之相关的词汇的新手。我现在正在网上学习一门课程,我试图勇敢地从视频中走出来,用不同的声音在不同的砖块上制作自己的脚本

我希望球在从桨上反弹时发出某种声音(“bip”)。然而,当我开始一个关卡时,在球还没发射之前,我就听到了“bip”的声音。在教程视频中,他加入了与球相关的声音,因此在更改了一些代码后没有这个问题。我的仍然存在。我已经有了我的球、砖块和桨的脚本。在我的球脚本中,我有一个
public bool hasStarted=false在我启动函数之前。我不想在每个脚本中都将
hasStarted
设置为私有bool。我只想从另一个脚本调用它,我也不想让它成为静态的,这样我就可以支持在一个关卡中有多个球

所以问题一:默认情况下,所有的
bool
都被认为是静态的吗

问题二:这是我的划桨脚本。启动关卡时,如何防止启动“bip”

public class Paddle : MonoBehaviour {

//private bool hasStarted = false;

//Use this for initialization
void Start () {

}

//Update is called once per frame
void Update(){
    Vector3 paddlePos = new Vector3 (0.5f, this.transform.position.y, 0f); //initial set position of the paddle on screen

    float mousePosInBlocks = Input.mousePosition.x / Screen.width * 16; //creates a variable called mousePosInBlocks to track
                                                                        //the position of the mouse X vector in game units

    paddlePos.x = Mathf.Clamp(mousePosInBlocks, 0.5f, 15.5f);   //setting the x vector of paddlePos e.g. the left/right function
                                                                //of the paddle to be the same as the mouse position on screen

    this.transform.position = paddlePos;    //the Object with the attached script (Paddle) is fed the updated
                                            //position info from the preceding 3 lines of code
}

void OnCollisionEnter2D(Collision2D col){
    // I need to do something with the code here to prevent a "bip" from starting before the ball is even launched
    // Find the game object which is called "Ball".
    GameObject ballObject = GameObject.Find("Ball");
    // Get the instance of the script, which is attached to the ball
    Ball ball = ballObject.GetComponent<Ball>();
    // Check if the ball has started
    if (ball.hasStarted) {
        audio.Play();
    }
}
公共类桨:单一行为{
//private bool hasStarted=假;
//用于初始化
无效开始(){
}
//每帧调用一次更新
无效更新(){
Vector3-pallepos=新的Vector3(0.5f,this.transform.position.y,0f);//屏幕上拨片的初始设置位置
float mousePosInBlocks=Input.mousePosition.x/Screen.width*16;//创建名为mousePosInBlocks的变量进行跟踪
//鼠标X向量在游戏单位中的位置
PapperPos.x=Mathf.Clamp(mousePosInBlocks,0.5f,15.5f);//设置PapperPos的x向量,例如左/右功能
//拨杆的位置与屏幕上的鼠标位置相同
this.transform.position=pallepos;//向带有附加脚本(palle)的对象提供更新的
//前3行代码中的位置信息
}
空心OnCollisionInter2D(碰撞2D列){
//我需要对这里的代码做一些处理,以防止在球发射之前启动“bip”
//找到称为“球”的游戏对象。
GameObject ballObject=GameObject.Find(“球”);
//获取附加到球的脚本实例
Ball=ballObject.GetComponent();
//检查球是否已启动
如果(球已开始){
音频播放();
}
}
}

请随时纠正我的笔记。我试着在写代码的时候做笔记,以便以后再回来时理解它


编辑:是一个pastebin屏幕,上面有桨和球脚本(谢谢Joonas)。

在您的音频源组件上,是否勾选了唤醒播放?首先,bool是类型,除非您声明为静态,否则它不是静态的。其次,在您的代码示例中,您正在调用Ball对象上的hasStarted公共字段,但我们看不到球的相关代码。Joonas是一个pastebin屏幕中球和桨的脚本。克里斯,在清醒状态下玩是不受限制的。我在试图解决这个问题的第一步就确定了这一点。我希望就这样。