1 Replies - 960 Views - Last Post: 24 August 2014 - 03:40 AM Rate Topic: -----

#1 Benzoate   User is offline

  • D.I.C Head


Reputation: 51
  • View blog
  • Posts: 234
  • Joined: 29-February 12

Object Reference is not set to an Instance of an object

Posted 23 August 2014 - 01:12 PM

UPDATE: I've determined that the Resourses isn't loading the AudioClips and I have no idea why..

Hello guys

I'm making a Audio Handler script that can play 5 different playlists but I keep getting an Object Reference error and I have no clue why. I've tried re working it for about 5 hours and I'm about to the point where I am just going to completely recode it, but I'd figure I'd see if you guys could help me before hand.

The error (right now) only seems to be appearing for the currentPlaylist audio clip array when I try to get the length so I can see if I need to keep going forward with tracks or restart from the beginning..

Here is the code I currently have
the AudioHandlerScript handles the current playlist, changing songs and such.

using UnityEngine;
using System.Collections;

public class AudioHandlerScript : MonoBehaviour {

	//should work. add clips to playlists and test

	PlaylistScript ps;

	public AudioClip[] listOne;
	public AudioClip[] listTwo;
	public AudioClip[] listThree;
	public AudioClip[] listFour;
	public AudioClip[] listFive;

	public AudioClip[] currentList;
	public int currentSong = 0;

	int playlistLength;

	float songLength;
	float playTime;

	public bool[] plBool = {true, false, false, false, false};

	// Use this for initialization
	void Start () {
		ps = gameObject.AddComponent<PlaylistScript>();
		ps.fixPLOne();
		ps.fixPLTwo ();
		ps.fixPLThree ();
		ps.fixPLFour ();
		ps.fixPLFive ();
		
		listOne = ps.playlistOne;
		listTwo = ps.playlistTwo;
		listThree = ps.playlistThree;
		listFour = ps.playlistFour;
		listFive = ps.playlistFive;
		currentList = listOne;
	
		print ("LO: "+listOne.Length);
		if(gameObject.GetComponent<AudioSource>() == null){
			gameObject.AddComponent<AudioSource>();
		}
		
		checkPlaylist ();
		playlistLength = (currentList.Length) - 1;
		currentSong = Random.Range (0, playlistLength);
		songLength = currentList [currentSong].length;

		audio.clip = currentList [currentSong];
		audio.Play ();
	}
	
	// Update is called once per frame
	void Update () {
		playTime += Time.deltaTime;

		if (playTime >= songLength) {
			changeTrack();
			print ("changeTrack() called from ahs update");
			playTime = 0;
		}
	}

	public void changeTrack(){
		checkPlaylist ();
		playlistLength = currentList.Length;
		currentSong++;
		
		if (currentSong > playlistLength) {
			currentSong = 0;
		}
		
		songLength = currentList [currentSong].length;
		audio.clip = currentList [currentSong];
		audio.Play ();
	}

	void checkPlaylist(){
		if (PlayerPrefs.GetString ("Playlist") == "One") {
			currentList = listOne;
		}else if (PlayerPrefs.GetString ("Playlist") == "Two") {
			currentList = listTwo;
		}else if (PlayerPrefs.GetString ("Playlist") == "Three") {
			currentList = listThree;
		}else if (PlayerPrefs.GetString ("Playlist") == "Four") {
			currentList = listFour;
		}else if (PlayerPrefs.GetString ("Playlist") == "Five") {
			currentList = listFive;
		}else{
			currentList = listOne;
		}
	}

	public void checkToggles(){
		if (PlayerPrefs.GetString ("Playlist") == "One") {
			plBool [0] = true;
			for (int i = 1; i < 5; i++) {
				plBool [i] = false;
			}
		} else if (PlayerPrefs.GetString ("Playlist") == "Two") {
			for(int i = 0; i < 5; i++){
				plBool[i] = false;
			}
			plBool[1] = true;
		} else if (PlayerPrefs.GetString ("Playlist") == "Three") {
			for(int i = 0; i < 5; i++){
				plBool[i] = false;
			}
			plBool[2] = true;
		} else if (PlayerPrefs.GetString ("Playlist") == "Four") {
			for(int i = 0; i < 5; i++){
				plBool[i] = false;
			}
			plBool[3] = true;
		} else if (PlayerPrefs.GetString ("Playlist") == "Five") {
			for(int i = 0; i < 5; i++){
				plBool[i] = false;
			}
			plBool[4] = true;
		}
	}
}


(The Check Toggles method is for GUI in another script)


the PlaylistScript that organizes the playlists
using UnityEngine;
using System.Collections;

public class PlaylistScript : MonoBehaviour {

	public AudioClip[] playlistOne;
	public AudioClip[] playlistTwo;
	public AudioClip[] playlistThree;
	public AudioClip[] playlistFour;
	public AudioClip[] playlistFive;
	
	string[] plOne = {"Chasing the Tuxedo.wav", "Choices on a Roll.wav", "Courage Buzz.wav",
			"Relaxation Reminds Me Of You.wav", "Revenge For Losers.wav", "Secret Sanity.wav",
			"Smooth Reasons.wav", "Thorny Sun.wav", "To The Top Yourself.wav"};
			
	string[] plTwo = {"5 Dollar Town.wav", "Addiction Of Peace.wav", "Brazen Samuari.wav",
			"Broken Psycho.wav", "Children's Television.wav", "Guilty Sauce.wav",
			"Sad Powers.wav", "Satin Space.wav", "Secret Sanity.wav"};
			
	string[] plThree = {"A Different Breed Of Game.wav", "Don't Talk Too Much.wav", "Futuristic Expressions.wav",
			"Persuasion Overdose.wav", "Revelations Knocking At My Door.wav", "Riot Of Scruitiny.wav",
			"Serious Case Of Abstraction.wav", "Taken Seriously.wav", "Taming Junk.wav"};
	
	string[] plFour = {"A Touch Of Your Bombshell.wav", "Acquired During the First Half.wav", "Addiction Of Peace.wav",
			"Chasing The Tuxedo.wav", "Dynamite Safari.wav", "Golden Samuari.wav",
			"Hateful Reasons.wav", "Jokes Are Ringing In My Ears.wav", "Sounds On A Roll.wav"};
	
	string[] plFive = {"Allergic To Demons.wav", "Don't Blame My Kisses.wav", "Forgotten Lies.wav",
			"Giving Em Movements.wav", "I've Only Got One Heart.wav", "One Night Of Progress.wav",
			"Serious Signals.wav", "Supernatural Jokes.wav", "The Beauty Of Junk.wav"};

	// Use this for initialization
	void Start () {
		fixPLOne ();
		fixPLTwo ();
		fixPLThree ();
		fixPLFour();
		fixPLFive ();
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	public void fixPLOne(){
		for(int i = 0; i < (plOne.Length)-1; i++){
			playlistOne[i] = Resources.LoadAssetAtPath <AudioClip>("Assets/Audio/Playlist One/"+plOne[i]);
		}
	}
	
	public void fixPLTwo(){
		for(int i = 0; i < (plTwo.Length)-1; i++){
			playlistTwo[i] = Resources.LoadAssetAtPath <AudioClip>("Assets/Audio/Playlist Two/"+plTwo[i]);
		}
	}
	
	public void fixPLThree(){
		for(int i = 0; i < (plThree.Length)-1; i++){
			playlistThree[i] = Resources.LoadAssetAtPath <AudioClip>("Assets/Audio/Playlist Three/"+plThree[i]);
		}
	}
	
	public void fixPLFour(){
		for(int i = 0; i < (plFour.Length)-1; i++){
			playlistFour[i] = Resources.LoadAssetAtPath <AudioClip>("Assets/Audio/Playlist Four/"+plFour[i]);
		}
	}
	
	public void fixPLFive(){
		for(int i = 0; i < (plFive.Length)-1; i++){
			playlistFive[i] = Resources.LoadAssetAtPath <AudioClip>("Assets/Audio/Playlist Five/"+plFive[i]);
		}
	}
}





Thank you guys for any help, I'm not expecting you to do it for me just point me in the right direction.



Also I'm using Unity and c# if that helps at all.

This post has been edited by Benzoate: 23 August 2014 - 03:00 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Object Reference is not set to an Instance of an object

#2 stayscrisp   User is offline

  • フカユ
  • member icon

Reputation: 1040
  • View blog
  • Posts: 4,325
  • Joined: 14-February 08

Re: Object Reference is not set to an Instance of an object

Posted 24 August 2014 - 03:40 AM

Post the full error please.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1