• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by nutso5000 · May 12 at 06:34 AM · audioscripting beginnerupdate functionno errors

How should I make this Audio play?

I was trying to make win and lose audios for my new game, and these audio will not play on the game over, only when I hit OnAwake to test. I have a bad game over and a good game over script. Please help me- it's my first game.

  using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class GameOver : MonoBehaviour
 {
     public GameObject GameOverPanel;
     public AudioSource audioSource;
     public AudioSource bgmusic;
   
 
     // Update is called once per frame
     void Update()
     {
         if(GameObject.FindGameObjectWithTag("Player") == null)
         {
           GameOverPanel.SetActive(true);
           bgmusic.Stop();
           audioSource.Play();
 
         }
 
     }
 
     public void Restart()
     {
       SceneManager.LoadScene(SceneManager.GetActiveScene().name);
     }
 }

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 public class WinManager : MonoBehaviour
 {
   // defines
   public GameObject GameOverPanel;
   public GameObject Image;
   public Text GameOverText;
   public Sprite Goodending;
   public float score = 5;
   public Sprite badending;
   public AudioSource win;
   public AudioSource bgmusic;
     // Update is called once per frame
 
   void Update()
   {
     if(GameObject.FindGameObjectWithTag("Player") != null)
     {
       score -= 1 * Time.deltaTime;
     }
     //condition for win, in this case running out the timer
     if(score < -5)
     {
       //win text
       GameOverText.text = "You Win! You got to school on time. P.S. I'm making more features later, so there's not much yet.";
       GameOverText.fontSize = 40;
       Image.GetComponent<Image>().sprite = Goodending;
       GameOverPanel.SetActive(true);
       bgmusic.Stop();
       win.Play();
     }
 
     else
     {
     GameOverText.text = "Game Over! You got a ticket and totaled your car.";
     GameOverText.fontSize = 65;
     Image.GetComponent<Image>().sprite = badending;
     }
   }
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by makled · May 12 at 09:53 AM

Hey,

There is so much to unpack here.

I'll first start by asking you if this code is in 1 single script or 2 separate scripts. If it is in 1 single script I would separate them into two different scripts.

Next thing, and this is the major problem is that you have to avoid using "FindObject" in Update methods. This is very expensive and is not best practice for your game. You could always have a reference to the Player on start and keep it. You don't need to check if the player is there every frame.

Regarding the sound, I need more information to pin point the problem. Which object is the audio source connected to?

What exactly happens to the player if you lose or win? Does it get destroyed? If it gets destroyed, do you have your audio sources on the player?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

187 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to an animation clip based on characters(letters) in an array 1 Answer

Audio clip change via script,changing audio clip in audio source via script 1 Answer

How to make dialogue system with voice 2 Answers

Continue Playing Music on Load Level 0 Answers

How can I call an audio clip in the update without it distorting?,How can i play an audio clip in the Update function? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges