• 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 H3lls7rik3r · Feb 28, 2015 at 10:17 PM · buildloadingaudioclipresources

Loading Audioclip from Resources fails in Build

Hey Guys,

I load Audioclips from the Resources folder into an Audioclip-List. In the editor it works fine, but in the Build not... "object reference not set to an ...". It seems like the Audiofiles are missing...

public List music; AudioClip curClip; float curPlayTime;

public void Start () {

  LoadMusic();
  
  ShowMenu(CurrentMenu);
  
  curClip = music[Random.Range(0, music.Count - 1)];
  audio.PlayOneShot(curClip);
  

}

void Update () {

PlayMusic();

}

void PlayMusic () {

  if(curClip.length < curPlayTime){
      curClip = music[Random.Range(0, music.Count - 1)];
      audio.PlayOneShot(curClip);
      curPlayTime = 0f;
  }
  
  curPlayTime += Time.deltaTime;
  

}

void LoadMusic () {

  DirectoryInfo dir = new DirectoryInfo("Assets/Resources/Music");
  FileInfo[] info = dir.GetFiles();
  foreach (FileInfo f in info){
      if(f.Name.EndsWith("mp3")){
          string path = f.ToString();
              Debug.Log(path);
              path = path.Remove(path.Length - 4, 4);
              path = path.Remove(0, path.Length - 15);
              path = path.Replace("\\", "/");
              Debug.Log(path);
          
          music.Add(Resources.Load<AudioClip>(path));
      }
  }
  

}

Thanks for the help :)

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

How can I include the resources folder to my project and build it to play on android devices? 1 Answer

Distribute terrain in zones 3 Answers

Altering prefab at runtime. Works in editor, not in build. 2 Answers

Loading a mesh? 1 Answer

Profile startup resources? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges