Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
1
Question by Voyliny · Apr 25, 2018 at 01:10 PM · bugaudiosourcebug-perhapsglitch

Audio doesn't want to play

I have been having so much trouble with this. After 5 hours of trying all kinds of things this idiotic thing still doesn't want to work. Then suddenly it worked and now it doesn't again -__-

This script is attached to the AudioSource. The times it get's through is 3.

this is the error I'm getting:

 NullReferenceException: Object reference not set to an instance of an object
 SoundManager.PlaySound (Int32 i, Int32 x) (at Assets/Scripts/PlayerScripts/SoundManager.cs:29)
 UIController.NButton () (at Assets/Scripts/PlayerScripts/UIController.cs:82)
 UIController.Start () (at Assets/Scripts/PlayerScripts/UIController.cs:35)

And this is the script.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SoundManager : MonoBehaviour
 {
     public AudioSource audioSource;
     public AudioClip AlarmClock;
     public static SoundManager instance = null;
 
     int times;
 
     void Awake()
     {
         if (instance == null)
             instance = this;
         else if (instance != this)
             Destroy(gameObject);
 
         DontDestroyOnLoad(gameObject);
     }
 
     public void PlaySound(int i, int x)
     {
         switch (i)
         {
             case 1:
                 // SFX Alarm clock
                 audioSource.clip = AlarmClock;
                 times = x; // Is 3
                 while (times >= 0)
                 {
                     if (!audioSource.isPlaying)
                     {
                         audioSource.Play();
                         times--;
                     }
                 }
                 break;
         }
     }
 }
 

I hope someone can help me to find the mistake. But at this point I am starting to think it's a bug :/

Comment
Add comment · Show 3
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 Voyliny · Apr 25, 2018 at 01:12 PM 0
Share

Even when adding the audio file directly to the clip area, it just doesn't want to play :/

avatar image Voyliny · Apr 25, 2018 at 02:02 PM 0
Share

When I use the function in the script itself, then everything is fine, but the moment that I have to call it from another script, it stop's working

avatar image vinilly Voyliny · Apr 25, 2018 at 04:33 PM 0
Share

Yeah I never use switch or while loops...

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by vinilly · Apr 25, 2018 at 04:31 PM

I think you can have a work around. I am not sure if alarm clock is the scenes music?

or a sound SFX from a clock. But I would just instantiate a prefab that auto destroys itself after a few with a clip

Example:

 public AudioClip clip;
 
     // Use this for initialization
     void Start ()
     {
         AudioSource.PlayClipAtPoint(clip, transform.position);
         Destroy(this.gameObject, 2.0f);
     }

This would come instantiated as a GameObject from prefab with a music clip. Can be called from any script.

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

143 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

Related Questions

Help with Fixed Joint , where the joint has a weird behavior. 0 Answers

Rigidbody / Animator Moving Y Position - bug? 1 Answer

Particle System not showing anything in its Component separated editor? 0 Answers

AvatarBuilder.BuildHumanAvatar set NaN positions with some avatars 1 Answer

3D Jumping Collision Bug 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