• 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
0
Question by nilesmac · Aug 05, 2020 at 05:51 AM · nullreferenceexceptionaudiosourcebuild and run

AudioManager not working in one of my scenes?

Edit: For some reason my images are not showing up in the post, but they are attached so you will have to click to view. It shows the error that the Developer build is saying, and the other image shows that I have the audiomanager with the sound effect in the scene attached.

There is one bug that only happens on BUILD AND RUN, it works fine when i am playing it in the editor, in one of my scenes that pertains to my audiomanager that I have spent all week trying to figure out why it is happening but I can not figure this out. My game has 7 scenes. In scene 4, two sound effects will NOT play because of this error:

![alt text][1]

Here it is in the log:

NullReferenceException: Object reference not set to an instance of an object at AudioManager.Play (System.String name) [0x00026] in C:\Users\Niles\Platformer\Assets\Scripts\AudioManager.cs:86 at LightSpell.SpellCast () [0x00001] in C:\Users\Niles\Platformer\Assets\Scenes\LightSpell.cs:26 at LightSpell.Update () [0x00021] in C:\Users\Niles\Platformer\Assets\Scenes\LightSpell.cs:43

What this tells me is that when I press the button for this ability, the sound effect is not in my AudioManager. Weird, since it is a prefab and works fine in the other scenes. ![alt text][2]

The Audio manager in the prefab does in fact have the sound effect name. [1]: /storage/temp/164776-capture.png [2]: /storage/temp/164777-capture.png

Here is the script for the ability sound effect

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 
 public class LightSpell : MonoBehaviour
 {
     public UnityEngine.Experimental.Rendering.Universal.Light2D SUN;
     public float countDown = 20.0f;
     public float downTime, upTime, pressTime = 0;
     public PlayerHealth playerHealth;
     public AudioManager b_audio;
     
 
     // Start is called before the first frame update
     void Start()
     {
         b_audio = FindObjectOfType<AudioManager>();
         playerHealth = GetComponent<PlayerHealth>();
         SUN.intensity = 0f;
         countDown = 10f;
     }
 
     public void SpellCast()
     {
         b_audio.Play("Nighteye");
             SUN.intensity = 2.5f;
         playerHealth.TakeMagic(1);
         
     }
 
     public void SpellExpired()
     {
         SUN.intensity = 0f;
     }
 
     // Update is called once per frame
     void Update()
     {
         
         if (Input.GetKeyDown(KeyCode.Q) && Time.timeScale != 0)
         {
             SpellCast();
             downTime = Time.time;
             pressTime = downTime + countDown;
         }
 
         if (Time.time >= pressTime)
         {
             SpellExpired();
             
         }
     }
 }

It calls the sound effect whenever the player pushes 'Q'. This works in all my other scenes.

Here is my AudioManager that has the array with the sound effect:

 using UnityEngine.Audio;
 using System;
 using UnityEngine;
 
 public class AudioManager : MonoBehaviour
 {
     public Sound[] sounds;
     public Sound[] Swordeffects;
     public Sound[] cutEffects;
     public Sound[] deathEffects;
 
     private AudioSource aSource;
     public static AudioManager instance;
 
     // Start is called before the first frame update
     void Awake()
     {
     
 
         foreach (Sound s in sounds)
         {
             s.source = gameObject.AddComponent<AudioSource>();
             s.source.clip = s.clip;
 
             s.source.volume = s.volume;
             s.source.pitch = s.pitch;
             s.source.loop = s.loop;
         }
 
         foreach (Sound s in Swordeffects)
         {
             s.source = gameObject.AddComponent<AudioSource>();
             s.source.clip = s.clip;
 
             s.source.volume = s.volume;
             s.source.pitch = s.pitch;
             s.source.loop = s.loop;
         }
 
         foreach (Sound s in cutEffects)
         {
             s.source = gameObject.AddComponent<AudioSource>();
             s.source.clip = s.clip;
 
             s.source.volume = s.volume;
             s.source.pitch = s.pitch;
             s.source.loop = s.loop;
         }
 
         foreach (Sound s in deathEffects)
         {
             s.source = gameObject.AddComponent<AudioSource>();
             s.source.clip = s.clip;
 
             s.source.volume = s.volume;
             s.source.pitch = s.pitch;
             s.source.loop = s.loop;
         }
 
 
 
     }
 
 
     public void PlayRandom()
     {
         Sound s = Swordeffects[UnityEngine.Random.Range(0, Swordeffects.Length)];
         s.source.Play();
     }
 
     public void PlayRandomDeath()
     {
         Sound s = deathEffects[UnityEngine.Random.Range(0, Swordeffects.Length)];
         s.source.Play();
     }
 
     public void PlayRandomCuts()
     {
         Sound s = cutEffects[UnityEngine.Random.Range(0, cutEffects.Length)];
         s.source.Play();
     }
 
     public void Play(string name)
     {
         Sound s = Array.Find(sounds, sound => sound.name == name);
         s.source.Play();
     }
 
     public void Pause(string name)
     {
         Sound s = Array.Find(sounds, sound => sound.name == name);
         s.source.loop = false;
     }
 }


What is happening is that the script is calling the array and playing the audio that matches the String name. If the string name was wrong, it wouldn't work in my other scenes, as both the AudioManager and the LightSpell are both prefabs in all of my scenes. For some reason, calls to the array of Sounds in the AudioManager is returning null. I have the AudioManager attached to every script that uses it. Even the one for the sound effect.

capture.png (51.8 kB)
capture.png (10.7 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

212 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Unity 2019.3.12f1 New Project Build NullReferenceException Causes only Background Color to Render 3 Answers

Unity can't load audioclip, but loads metadata from same class. 0 Answers

Null Reference Exception For An Unknown Reason 0 Answers

Null Reference Exception on an unused script 1 Answer

C# Multidimensional arrays 0 Answers


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