Open Door Audio

Hello i have a problem my script play AudioSource more and more times … does anyone know why?

Here is my script :

sing System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class OpenDoorBoolean : MonoBehaviour
{
    public float rotateR;
    public float rotateL;
    public float stoprotate = 80;
    public  bool openDoorBool = false;
    public AudioSource OpenDoor;
  

    public GameObject Ldoor;
    public GameObject Rdoor;
    public Transform camera;
    public GameObject Spawn;
    public bool camerabool = false;
    void Start()
    {
      
    }

    // Update is called once per frame
    void Update()
    {
        if (openDoorBool == true)
        {
            rotateR += 1 * Time.deltaTime * 50;
            rotateL -= 1 * Time.deltaTime * 50;
            Ldoor.transform.rotation = Quaternion.Euler(0, rotateL, 0);
            Rdoor.transform.rotation = Quaternion.Euler(0, rotateR, 0);
            if (camerabool == true && OpenDoor.isPlaying == false)
            {
                camerabool = false;
                OpenDoor.Play();
                StartCoroutine(cameraspawn());
           
              
            }
        }

       

        if (rotateR > stoprotate)
        {
            openDoorBool = false;
        }
        IEnumerator cameraspawn()
        {
            yield return new WaitForSeconds(0.5f);
            Instantiate(camera, Spawn.transform.position, Spawn.transform.rotation);
        }
    }


}

Hi,

What is the IEnumerator cameraspawn() doing in the Update() loop?

Hi i dont know but working good …sorry I am a beginner

You can make a test with Debug.Log(“Write what you want!”) in the if-statement in line 33. Write in the brackets the Debug…
Write me back if the console said something

if i write in line 33 if (camerabool == true && OpenDoor.isPlaying == false) Debug.Log(“Debug…”);
my camera raspawn 500 times and console nothing write

Do not write it like this:

Make it like this:

if (camerabool == true && OpenDoor.isPlaying == false)
{
  Debug.Log( "hit!");
  camerabool = false;
  OpenDoor.Play();
  StartCoroutine(cameraspawn());
}

The if statement only affects ONE thing after it, in your case the the Debug.Log().

But if you put it in braces, it affects the entire brace block.

Hi I tried, it did the same …only 1 times write Hit or Debug…

I already know error what it is

i push openDoorBool with OnTriggerstay …if i will use OnTriggerEnter script work good but i need OnTriggerStay.

OnTriggerStay works on another script this sount. why?

[/code][/QUOTE]

You can test with debug … if the boolean are right. Maybe the statement don’t play because of it.
So test the boolean and then you can go even further.