Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Object wont play sound when hit coin and afterwards switch to level 02

Discussion in 'Scripting' started by Deleted User, Apr 4, 2014.

  1. Deleted User

    Deleted User

    Guest

    Hi everyone! It seems like my ball won't play a sound and switch level after i collect the 7 coins.


    GameMaster script

    Code (csharp):
    1. #pragma strict
    2.  
    3. static var currentScore : int = 0;
    4.  
    5. var SwitchLevel1 : SwitchLevel1;
    6.  
    7. var offsetY : float = 40;
    8. var sizeX : float = 100;
    9. var sizeY : float = 40;
    10.  
    11. function Start () {
    12.     currentScore = 0;
    13.     SwitchLevel1 = GameObject.Find ("Ball").GetComponent.<SwitchLevel1>();
    14.  
    15. }
    16. function OnGUI () {
    17.     GUI.Box(new Rect (Screen.width/2-sizeX/2, offsetY, sizeX, sizeY), "Score: " + currentScore);
    18. }
    19.  
    20. function AddScore () {
    21.  
    22.     ++currentScore;
    23.  
    24.     if (currentScore == 7) {
    25.  
    26.         StartCoroutine (SwitchLevel1.ReachedPassScore ());
    27.  
    28.     }
    29.  
    30. }
    31.  
    SwitchLevel script


    Code (csharp):
    1. #pragma strict
    2.  
    3.  
    4.  
    5. // This function is called from GameMaster
    6.  
    7.  
    8.  
    9. var WinningSound : AudioClip;
    10.  
    11.  
    12.  
    13. function ReachedPassScore () {
    14.  
    15.  
    16.  
    17.     yield StartCoroutine (PlaySound ());
    18.  
    19.     GameMaster.currentScore = 0;
    20.  
    21.     Application.LoadLevel ("Level02");
    22.  
    23.  
    24.  
    25. }
    26.  
    27.  
    28.  
    29.  
    30.  
    31.  
    32.  
    33. function PlaySound () {
    34.  
    35.  
    36.  
    37.     audio.clip = WinningSound;
    38.  
    39.     audio.Play ();
    40.  
    41.     yield WaitForSeconds (audio.clip.length);
    42.  
    43.  
    44.  
    45. }
    I have attached my Ball/GameObject under my GameMaster

    Here is a screenshot of it in Unity
    $Unavngivet.jpg $Unavngivet.jpg

    Whenever i click the scripty thing/ball it just goes to the ball/gameobject? Why wont it play the sound???
    Picture of it again afterwards i clicked the script/ball

    $Unanvngivet1.jpg


    If you need the other scripts just tell me. I just want to find the error! :I
     
    Last edited by a moderator: Apr 4, 2014
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
  3. Deleted User

    Deleted User

    Guest

    I will sorry
     
  4. Deleted User

    Deleted User

    Guest

    I have changed it now :p
    If you come up with any solution please tell me :D
     
  5. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    This is a fairly simple scenario. Did you try to debug it yourself? Just place some Debug.Log at the positions where you think something may go wrong. If the timing is wrong, you may additionally output Time.realtimeSinceStartup to keep track of that.
    As you are not sure about the sound playback, place a Debug.Log ("Starting playback " + Time.realtimeSinceStartup); before the playback and another one at the end to find out what is happening.
     
  6. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,304
    Stop. Double. Posting.
     
  7. Deleted User

    Deleted User

    Guest

    I debugged every line and this there's is something wrong with this line :I
    It appears everytime i start the level this line of code show how many seconds i survived the last game before the GameOver Sound was played


    Code (csharp):
    1. SwitchLevel1 = GameObject.Find ("Ball").GetComponent.<SwitchLevel1>();
    2. Debug.Log ("Starting playback " + Time.realtimeSinceStartup);
    Here is the full Function. It is under the GameMaster.js script

    Code (csharp):
    1. function Start () {
    2.     currentScore = 0;
    3.     SwitchLevel1 = GameObject.Find ("Ball").GetComponent.<SwitchLevel1>();
    4.      Debug.Log ("Starting playback " + Time.realtimeSinceStartup);