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 /
This question was closed Jan 03, 2016 at 10:05 PM by TabuuForteAkugun for the following reason:

I got it. I never preloaded the SongSlots script.

avatar image
0
Question by TabuuForteAkugun · Dec 22, 2015 at 01:03 AM · c#audiosourceaudioclip

[Got it!]Classes keep spitting out null values (trying to play a looped song)

Hi.

In my game, I wrote a system where I can use 2 audio clips and sync them together to play one looped song, like in console games.

The song clips, as well as metadata for info and playback, are here:

 using UnityEngine;
 using System.Collections;
 
 public class Song {
 
     public string gameTitle;            // Title of the track.
     public string sourceGame;            // Game the track comes from ("Original" for tracks composed for Feud).
     public bool introPlayed;            // Used for update.
     public bool canLoop;                // If it is a looping song.
     public AudioClip intro;                // The AudioClip containing the intro of the song.
     public AudioClip loop;                // The looping track.    
     public Sprite franchise;            // Franchise icon i.e. Mushroom for Mario songs.
     public string arrangement;            // Arrangement or Original?
     public string[] description;        // Array to contain the song's "description".
     public bool unlocked;                // For hidden songs: if they are unlocked or not.
     public bool isDefault;                // Default song (to check when Erasing Data).
 
     // Use this for initialization
     public Song (string title, string src, bool loopable, AudioClip beg, AudioClip loo, Sprite fran, string kind, string desc1, 
                      string desc2, string desc3, string desc4, bool unhide, bool def) {
         gameTitle = title;
         sourceGame = src;
         canLoop = loopable;
         intro = beg;
         loop = loo;
         franchise = fran;
         arrangement = kind;
         description [0] = desc1;
         description [1] = desc2;
         description [2] = desc3;
         description [3] = desc4;
         unlocked = unhide;
         isDefault = def;
     }
 
     // If this is a hidden song, unlock it.
     public void unlock () {
         unlocked = true;
     }
 
     public void relock () {
         unlocked = false;
     }
 }

So, this doesn't throw any errors. In another class, I have a Song array with Song objects for every track in my game. Song.cs doesn't inherit from MonoBehaviour because it's just a container.

 using UnityEngine;
 using System.Collections;
 
 public class SongSlots: MonoBehaviour {
 
     public Song[] songSlots;    // This array contains the entire music soundtrack and its metadata, using stuff from the Song class.
 
     public static SongSlots Instance{get; set;}
 
     void Awake()
     {
         songSlots = new Song[150]; // Set the array to the exact number of complete tracks in the game.
         // Set the metadata for ALL songs then return the instance of SongSlots.
 
 [over 100 Song slots...]
 
         Instance = this;
     }
 
     // While the array is full already, get actual song count by using "unlocked". This is for the Sound Test.
     public int getTrueCount()
     {
         int count = 0;  // Song count will be here.
         // Calculate songs based on unlock status.
         for (int x = 0; x < songSlots.Length; ++x)
         {
             if (songSlots[x].unlocked)
             {
                 // Increment "count" ONLY if songSlots[x].unlocked is true.
                 ++count;
             }
         }
         return count;
     }
 }

Again, no issues. Then I wrote this algorithm for playback:

 using UnityEngine;
 using System.Collections;
 
 public class LoopBGM : MonoBehaviour
 {
     public bool isSetUp;                     // To avoid constant resetting of playback to 0.
     public bool introPlayed;                 // To avoid playing the intro more than once.
 
     public static LoopBGM Instance { get; set; }
 
     void Awake()
     {
         Instance = this;
     }
 
     // Main src handler. Song object for the song. Assumes AudioSource is looped.
     public void playBGM(AudioSource src, Song track)
     {
         if (!introPlayed)
         {
             if (!isSetUp)
             {
                 src.clip = track.intro;
                 src.Play();
                 isSetUp = true;
             }
             if (src.time == src.clip.length && track.canLoop == true && track.loop != null)
             {
                 isSetUp = false;
                 if (!isSetUp)
                 {
                     src.clip = track.loop;
                     src.Play();
                     isSetUp = true;
                 }
                 introPlayed = true;   // Break out of if statement and leave src alone.
             }
         }
     }
 }

Goes all right. Finally, I wrote a class to test this system in a test scene used to animate GameObjects.

 using UnityEngine;
 using System.Collections;
 
 public class BGM_TEST : MonoBehaviour
 {
     public AudioSource tst;
     public Song sng;
     public AudioClip clip;
 
     // Setup the AudioSource.
     void Awake()
     {
         tst = GetComponent<AudioSource>();
         sng = SongSlots.Instance.songSlots[0];
         clip = sng.intro;
     }
     void Update()
     {
         // Test while playing the main menu theme
         LoopBGM.Instance.playBGM(tst, sng);
     }
 }

And I hit the test button above the Scene view. (Building crashes Unity 5.3 for now) The console keeps throwing null values and thus cannot play the intro clip in my Song object. I double checked the SongSlots array and everything is set up properly. AudioClips are set up using Resources.Load("Song Path") as AudioClip, and the OGG files are in a Resources folder. The Intro AudioClip never gets passed to my AudioSource component.

I'd really like to know why the heck it's not working.

If there's anything else you need to see, let me know.

Comment
Add comment · Show 1
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 TabuuForteAkugun · Jan 03, 2016 at 10:04 PM 0
Share

Never $$anonymous$$d. I think I figured it out: I never attached the script to anything and assumed the global variable would create itself when the game was running.

I worked a bit on saving functions recently and was perfectly able to save elements from the List without it throwing Nulls. I think I should try now to play the tracks.

Thanks for the help. ^_^

2 Replies

  • Sort: 
avatar image
0

Answer by Firedan1176 · Dec 22, 2015 at 01:49 AM

The problem is your arrays. 1.) In your Song class, replace:

public string[] description with public string[] description = new string[4];

With arrays, you not only have to declare it with string[] description, but you must also initialize it to allocate memory. Therefore, when you try to set description[0] = ... later, it gives you an error because you don't initialize it! You'll also need to do that for all your other arrays.

If I were you, I would initialize it when you declare it.

If you're using "song slots", I'd use Lists so that you can change the size, instead of having to give it a fixed size.

If that doesn't fix the problem, tell me what other errors you get, word for word.

Comment
Add comment · Show 3 · 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 TabuuForteAkugun · Dec 22, 2015 at 01:56 AM 0
Share

Didn't fix it. The issue seems to be that it never assigns the AudioClips.

I also get this before the null AudioClip error:

NullReferenceException: (wrapper stelemref) object: stelemref (object.intptr.object)

avatar image Firedan1176 TabuuForteAkugun · Dec 22, 2015 at 07:44 PM 0
Share

When are you actually assigning the song slots?

avatar image TabuuForteAkugun Firedan1176 · Dec 22, 2015 at 07:49 PM 0
Share

I assign them in the Awake method. But I'm really not sure how to call it.

avatar image
0

Answer by TabuuForteAkugun · Dec 22, 2015 at 07:17 PM

Yep. I tried a variety of solutions and nothing works: it just won't get the AudioClip.

I think it's the List itself that returns null, because when I call it, that's when it throws the error.

Since my List is in a Singleton class, how would one get data from it at runtime?

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

Follow this Question

Answers Answers and Comments

43 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

Related Questions

Second AudioClip won't play 0 Answers

How to make an editor extension to unity ui for switching audio clips? 0 Answers

Second AudioClip won't play 0 Answers

AudioClip not playing 0 Answers

My audio doesn't play, I'm through my options... 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