Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

AudioSource play when 2 variables are true

Discussion in 'Audio & Video' started by crawl55, Jan 16, 2016.

  1. crawl55

    crawl55

    Joined:
    Jan 17, 2014
    Posts:
    8
    The end state I want is for certain audio clips to play when components are active in the hierarchy. I have a script that turns each component on and off given certain variables.

    The problem is with the code that I have, the audio "LightOff" occurs one click late. Meaning, if statement is true, it takes one more click for the script to play the audiosource.

    I am sure there is a more efficient way to achieve the end result, but I am pretty new at C# and this is the best I can come up with.

    Any advice would be greatly appreciated. Thanks in advance.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Start_Switch : MonoBehaviour {
    6.  
    7.     public GameObject ThrottleOff;
    8.     public GameObject ThrottleIdle;
    9.     public GameObject ThrottleFly;
    10.     public GameObject ThrottleReturnIdle;
    11.     public GameObject StartSwitch;
    12.  
    13.     public AudioSource StarterEngage;
    14.     public AudioSource LightOff;
    15.     public AudioSource StarterEngageCoastDown;
    16.  
    17.     void Start ()
    18.     {
    19.    
    20.     }
    21.     void Update ()
    22.     {
    23.         if (StartSwitch.activeInHierarchy == true && ThrottleIdle.activeInHierarchy == true)
    24.         {
    25.             LightOff.Play ();
    26.         }
    27.     }
    28.  
    29.     void OnMouseDown ()
    30.     {
    31.         if (StartSwitch.activeInHierarchy == false)
    32.             {
    33.             StartSwitch.SetActive (true);
    34.             StarterEngage.Play ();
    35.             }
    36.             else
    37.             {
    38.                     StartSwitch.SetActive (false);
    39.                     StartCoroutine ("WaitOneSecond");
    40.                
    41.             }
    42.     }
    43.  
    44.     IEnumerator WaitOneSecond()
    45.     {
    46.         yield return new WaitForSeconds (1);
    47.         StarterEngageCoastDown.Play ();
    48.         StartCoroutine ("WaitOneMoreSecond");
    49.     }
    50.     IEnumerator WaitOneMoreSecond()
    51.     {
    52.         yield return new WaitForSeconds (0.3f);
    53.         StarterEngage.Stop ();
    54.     }
    55. }
    56.  
    57.  
     
  2. wheatgrinder

    wheatgrinder

    Joined:
    Mar 20, 2015
    Posts:
    21
    if you take out the "&& ThrottleIdle.activeInHierarchy==true"
    does it work as expected?
     
  3. bphillips09

    bphillips09

    Joined:
    Aug 31, 2014
    Posts:
    36
    Have you tried using .activeSelf instead of .activeInHierarchy? I think .activeInHierarchy only applies if the GameObject AND all of its parents are active.