unity菜鸟错误:script多挂载,乱挂载导致的同名分支问题

写了一晚上作业,里面一个脚本挂到了空物体上再去添加onclik事件,这个脚本呢,有两个public audio clip。由于还有其他函数,它还被挂到了canvas上。
题外话:保存一下音频播放切换停止静音的代码先`

AudioSource audioctrl;
GameObject textobj;
bool isplay = false;
bool isStop = false;
bool isMute = false;
GameObject Button;

bool i=false;
public AudioClip[] audios=new AudioClip[2];


// Start is called before the first frame update

void Start()
{
    audioctrl = GameObject.Find("Canvas").GetComponent<AudioSource>();
    textobj = GameObject.Find("openplay");

    Button= GameObject.Find("open");
    GameObject.Find("Canvas").GetComponent<AudioSource>().clip = audios[0];
    Debug.Log(audioctrl.mute);


}

// Update is called once per frame
void Update()
{
    
}
public void anothersongplay()
{
    if (!i) i = true;
    else i = false;
    if (i)
        audioctrl.clip = audios[1];
    else
        audioctrl.clip = audios[0];
    GameObject.Find("openplay").GetComponent<Text>().text = "播放";
    GameObject.Find("stopplay").GetComponent<Text>().text = "暂停";
    bool isplay = false;
    bool isStop = false;
    bool isMute = false;


}
public void openmusic()
{
    Button = GameObject.Find("open");
    textobj = Button.transform.Find("openplay").gameObject;
    if (!isplay)
    {
        audioctrl.Play();
        textobj.GetComponent<Text>().text = "正在播放";

    }
    else
    {
        audioctrl.Stop();
        textobj.GetComponent<Text>().text = "播放";
    }
    isplay = !isplay;



}
public void stopmusic()
{
    Button = GameObject.Find("stop");
    textobj = Button.transform.Find("stopplay").gameObject;
    if (isplay)
    {

        if (!isStop)
        {
            audioctrl.Pause();
            textobj.GetComponent<Text>().text = "继续";

        }
        else
        {
            audioctrl.Play();
            textobj.GetComponent<Text>().text = "暂停";
        }
        isStop = !isStop;
    }



}
public void musicmute()
{
    Button = GameObject.Find("mute");
    textobj = Button.transform.Find("muteplay").gameObject;

    if (!isMute)
    {
        audioctrl.mute=true;
        GameObject.Find("stop").GetComponent<Button>().interactable = false;
        GameObject.Find("open").GetComponent<Button>().interactable = false;
        textobj.GetComponent<Text>().text = "恢复正常";

    }
    else
    {
        audioctrl.mute=false;
        GameObject.Find("stop").GetComponent<Button>().interactable = true;
        GameObject.Find("open").GetComponent<Button>().interactable = true;
        textobj.GetComponent<Text>().text = "静音";
    }
    isMute = !isMute;
}

}
于是喜闻乐见的我往canvas上的script疯狂添加音频,根本没想起来空物体上的脚本函数才是被调用的那个。简单来说,以为多挂载还是引用同一个类而已。但不是,至少public不是,得记住派生关系。
还是个不懂类是啥的菜鸟啊。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值