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. }
    SwitchLevel script

    Code (csharp):
    1. #pragma strict
    2.  
    3. // This function is called from GameMaster
    4.  
    5. var WinningSound : AudioClip;
    6.  
    7. function ReachedPassScore () {
    8.  
    9.     yield StartCoroutine (PlaySound ());
    10.     GameMaster.currentScore = 0;
    11.     Application.LoadLevel ("Level02");
    12.  
    13. }
    14.  
    15.  
    16.  
    17. function PlaySound () {
    18.  
    19.     audio.clip = WinningSound;
    20.     audio.Play ();
    21.     yield WaitForSeconds (audio.clip.length);
    22.  
    23. }
    I have attached my Ball/GameObject under my GameMaster

    Here is a screenshot of it in Unity
    $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
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Test.

    Move the play co routine into the reach passed score. Since it is yielding anyways no need for a second corotine. Comment out the load level and print a response. If the sound goes off and the print happens it is good. If the sound never plays you have other problems.
     
  3. Deleted User

    Deleted User

    Guest

    Could you maybe script this? Im really new to Unity so i'm not so good at it yet.
     
  4. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Code (csharp):
    1.  
    2. var WinningSound : AudioClip;
    3.  
    4. function ReachedPassScore () {
    5.     audio.clip = WinningSound;
    6.     audio.Play ();
    7.     yield WaitForSeconds (audio.clip.length);
    8.     GameMaster.currentScore = 0;
    9.     //Application.LoadLevel ("Level02");
    10.     print "TEST LEVEL End!!!!";
    11. }
    12.