• 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 KingGolem · May 30, 2015 at 05:21 PM · objectcheckexists

Check if a 2D Object Exists/or not; C#

I am making a Breakout clone as a learning experience, and I am trying to make the game play a failure sound when there are no balls left on screen. I created an empty game object to check if any of the three (possible) balls exist, and play the sound if they don't. Here is the script I used:

 using UnityEngine;
 using System.Collections;
 
 public class BallCounter : MonoBehaviour {
     AudioSource audio;
     Object ball1; 
     Object ball2; 
     Object ball3;
     // Use t$$anonymous$$s for initialization
     void Start () {
         audio = GetComponent<AudioSource> ();
     }
     
     // Update is called once per frame
     void Update () {
         ball1 =  GameObject.Find ("Ball");
         ball2 =  GameObject.Find ("Ball 1");
         ball3 =  GameObject.Find ("Ball 2");
         if (ball1 || ball2 || ball3) {
         } else {
             audio.Play ();
         }
     }
 }


Sadly, it doesn't work. What am I doing wrong?

Comment
Add comment · Show 4
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
avatar image maccabbe · May 30, 2015 at 05:34 PM 0
Share

Does the else code run and you can't hear the audio or does the else code never run?

avatar image buggy213 · May 30, 2015 at 06:03 PM 0
Share

Is it possible that you are not actually destroying the ball if it leaves the screen? Or that you have not assigned an AudioClip?

avatar image KingGolem · May 31, 2015 at 12:52 AM 0
Share

I put in a Debug.Log message in the else code to test if it runs, and yes, it certainly does. I also confirmed that I have indeed assigned an AudioClip and that the ball is destroyed when it leaves the screen, as it comes in contact with a collider object that destroys it on contact. I'm led to believe this is some problem with audio.Play().

avatar image maccabbe · May 31, 2015 at 02:44 AM 0
Share

If the sound is currently 3D then the audiolistener might just might be too far from the audiosource. Try changing the sound to 2D. If you can hear it 2D then you can keep it 2D or change the audiosource settings so 3D sounds can be heard from further away.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by KingGolem · May 31, 2015 at 11:45 AM

Oh hey, I just solved it. The audio source works, but for some reason audio.Play() doesn't want to work wit$$anonymous$$n an else statement. So I just made the else statement set a bool to true to trigger another if statement, in w$$anonymous$$ch I put audio.Play(). Here's what I came up with:

 using UnityEngine;
 using System.Collections;
 
 public class BallCounter : MonoBehaviour {
     AudioSource audio;
     public AudioClip FailureBuzz;
     Object ball1; 
     Object ball2; 
     Object ball3;
     bool playSound = false;
     bool gameOver = false;
     // Use t$$anonymous$$s for initialization
     void Start () {
         audio = GetComponent<AudioSource> ();
     }
     
     // Update is called once per frame
     void Update () {
         ball1 =  GameObject.Find ("Ball");
         ball2 =  GameObject.Find ("Ball 1");
         ball3 =  GameObject.Find ("Ball 2");
         if (ball1 || ball2 || ball3) {
             Debug.Log ("Balls exist");
         } else {
             playSound = true;
             //audio.PlayOneShot (FailureBuzz);
             Debug.Log ("Balls don't exist");
         }
         if ((playSound == true) && (gameOver == false)) {
             audio.Play ();
             gameOver = true;
         }
     }
 }


It's not exactly elegant, and I still have no idea why audio.Play() won't run in an else statement, but t$$anonymous$$s code works, and that's all that matters. Thank you all for helping me debug.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Check object for motion 1 Answer

Check if file exists on Internet 2 Answers

How would I check if an object is in the scene? 1 Answer

Check if var is true from a random object 0 Answers

Test if Abstract Class Exists 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