Search Unity

Open Door Audio

Discussion in 'Scripting' started by brano42, Sep 25, 2021.

  1. brano42

    brano42

    Joined:
    Aug 25, 2020
    Posts:
    78
    Hello i have a problem my script play AudioSource more and more times ..... does anyone know why?

    Here is my script :
    Code (CSharp):
    1. sing System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class OpenDoorBoolean : MonoBehaviour
    6. {
    7.     public float rotateR;
    8.     public float rotateL;
    9.     public float stoprotate = 80;
    10.     public  bool openDoorBool = false;
    11.     public AudioSource OpenDoor;
    12.  
    13.  
    14.     public GameObject Ldoor;
    15.     public GameObject Rdoor;
    16.     public Transform camera;
    17.     public GameObject Spawn;
    18.     public bool camerabool = false;
    19.     void Start()
    20.     {
    21.      
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.         if (openDoorBool == true)
    28.         {
    29.             rotateR += 1 * Time.deltaTime * 50;
    30.             rotateL -= 1 * Time.deltaTime * 50;
    31.             Ldoor.transform.rotation = Quaternion.Euler(0, rotateL, 0);
    32.             Rdoor.transform.rotation = Quaternion.Euler(0, rotateR, 0);
    33.             if (camerabool == true && OpenDoor.isPlaying == false)
    34.             {
    35.                 camerabool = false;
    36.                 OpenDoor.Play();
    37.                 StartCoroutine(cameraspawn());
    38.            
    39.              
    40.             }
    41.         }
    42.  
    43.        
    44.  
    45.         if (rotateR > stoprotate)
    46.         {
    47.             openDoorBool = false;
    48.         }
    49.         IEnumerator cameraspawn()
    50.         {
    51.             yield return new WaitForSeconds(0.5f);
    52.             Instantiate(camera, Spawn.transform.position, Spawn.transform.rotation);
    53.         }
    54.     }
    55.  
    56.  
    57. }
    58.    
     
  2. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    782
    Hi,

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

    brano42

    Joined:
    Aug 25, 2020
    Posts:
    78
    Hi i dont know but working good ....sorry I am a beginner
     
  4. glgamesforfun

    glgamesforfun

    Joined:
    Apr 13, 2021
    Posts:
    149
    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
     
  5. brano42

    brano42

    Joined:
    Aug 25, 2020
    Posts:
    78
    if i write in line 33 if (camerabool == true && OpenDoor.isPlaying == false) Debug.Log("Debug...");
    my camera raspawn 500 times and console nothing write
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Do not write it like this:

    Make it like this:

    Code (csharp):
    1. if (camerabool == true && OpenDoor.isPlaying == false)
    2. {
    3.   Debug.Log( "hit!");
    4.   camerabool = false;
    5.   OpenDoor.Play();
    6.   StartCoroutine(cameraspawn());
    7. }
    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.
     
  7. brano42

    brano42

    Joined:
    Aug 25, 2020
    Posts:
    78
    Hi I tried, it did the same ...only 1 times write Hit or Debug...
     
  8. brano42

    brano42

    Joined:
    Aug 25, 2020
    Posts:
    78
    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?
     
  9. glgamesforfun

    glgamesforfun

    Joined:
    Apr 13, 2021
    Posts:
    149
    [/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.