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
0
Question by Docyoda · Jan 20, 2018 at 12:29 AM · errorerror messageobject referenceobject-reference-error

PLEASE HELP!! Object reference not set to an instance of an object at ChangeMasterVolume.Update

FIXED!

Here's how I fixed the problem.


 void Update () {
         if (VolumeSlider != null) {
 
             myMusic.volume = VolumeSlider.value;
 
         }



I made it so if the VolumeSlider (Slider) was equal to null, it would run the same thing as before (myMusic.volume = VolumeSlider.value). Thanks to @IvanDrake


Info


So I'm making a slider that changes the overall volume of my game. The only music that is being used in the actual game is some background music, and that's it. No other sounds or anything.

This is the code that I am using for an empty object in my Main Menu, where the slider is located.


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class ChangeMasterVolume : MonoBehaviour {
 
     public Slider MasterVolume;
     public AudioSource myMusic;
 
     void Update () {
 
         myMusic.volume = MasterVolume.value;
 
     }
 }



Here's what the inspector of the empty object (AKA MusicManager) that contains the code looks like: alt text


Problem


And I'm getting this error in the console when I press the play button:

NullReferenceException: Object reference not set to an instance of an object at ChangeMasterVolume.Update ()


Extra Info


Also I would like to note that I recently added another script called "Continuous Music" that allows the music to continuously play between scenes. Not sure if this is relevant, but I'll add the script anyway:

 using System.Collections;
 using UnityEngine;
 
 public class ContinuousMusic : MonoBehaviour {
 
     void Awake () {
     
         GameObject[] objs = GameObject.FindGameObjectsWithTag ("music");
         if (objs.Length > 1)
             Destroy (this.gameObject);
         
         DontDestroyOnLoad (this.gameObject);
     }
 }



screen-shot-2018-01-19-at-71730-pm.png (56.2 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

1 Reply

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

Answer by IvanDrake · Jan 20, 2018 at 01:50 AM

There may be some conflict between the two scripts. Maybe set your MasterVolume slider by code, rather than manually. If the music manager is being destroyed, the new one may not be set.

Also, might be good to add an 'if' statement before setting the volume value to check that the slider exists. You can add an 'else' with a custom error message to add more detail. That way it won't freeze the game and will give you a better idea of what's going wrong.

Comment
Add comment · Show 1 · 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
avatar image Docyoda · Jan 20, 2018 at 01:53 AM 0
Share

Thank you so much! Here's what I did:

 void Update () {
         if (VolumeSlider != null) {
 
             my$$anonymous$$usic.volume = VolumeSlider.value;
 
         }

Fixed the problem! Thanks!

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

99 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

Related Questions

Array of dictionaries: "Object reference not set to an instance of an object" 0 Answers

[Begginer] [Solved] How to solve a Null Reference Exception 1 Answer

Error Object reference not set to an instance of an object!!! Help, I am new! 0 Answers

0xc000007b error on 64 bit machine? 1 Answer

My Unity Project says i have compile errors but they won't show up! please Help!,My unity project says I have compile errors but they aren't showing up at the bottom of my screen! Please Help!!1 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