我想到了。基本上,这个脚本在一个空对象上,必须检查bulbcount是否为8,我将检查移到另一个游戏对象并将音频播放功能留在同一个地方。我还将 onEnabled 用于将播放声音的游戏对象,并在一开始使其处于非活动状态。
因此,当另一个游戏对象(检查灯泡计数所在的位置)知道灯泡计数为 8 时,它将启用音频播放器,并且 (OnEnabled) 它将播放声音。
这是另一个游戏对象(检查灯泡计数的对象)上的脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine;
public class LampScript : MonoBehaviour
{
private bulbmanager bulbreq;
public GameObject wincheck;
//private bulbsmustcollect bulblvl;
// Start is called before the first frame update
void Start()
{
bulbreq = FindObjectOfType<bulbmanager>();
//bulblvl = FindObjectOfType<bulbsmustcollect>();
}
// Update is called once per frame
void Update()
{
if (bulbreq.bulbcount >= 8)
{
wincheck.gameObject.SetActive(true);
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
if (bulbreq.bulbcount >= 8)
{
//loop.Stop();
SceneManager.LoadScene("Level 2");
}
}
}
}
这是我在音频播放器中输入的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class checkifdone : MonoBehaviour
{
public AudioSource checkwin;
public GameObject wincheck;
// Start is called before the first frame update
void Start()
{
wincheck.gameObject.SetActive(false); //This is the same object where script is.
}
private void OnEnable()
{
checkwin.Play();
}
}
当然,我们不要忘记分配所需的一切。音频现在播放到完整,因为它独立于其他脚本文件的更新功能。不确定这是否是最好的方法,但它有效!