- Home /
Jukebox parsing error
Okay so I know what the code is for (the jukebox player) but for some reason I keep getting a parsing error. Here is what I have, what am I doing wrong? It is in C#.
using UnityEngine; using System.Collections;
JukeboxController jukebox = new JukeboxController(); jukebox.AddClip("mysong", myclip); jukebox.PlayClip("mysong"); jukebox.StopClip();
public class JukeboxController {
Hashtable jukebox;
string current_clip;
public JukeboxController()
{
jukebox = new Hashtable();
current_clip = null;
}
// constructor
public void AddClip(string name, AudioClip clip)
{
if (jukebox == null)
{
jukebox = new Hashtable();
} // if
GameObject obj;
obj = new GameObject();
obj.AddComponent("AudioSource");
obj.audio.clip = clip;
obj.audio.ignoreListenerVolume = true;
DontDestroyOnLoad(obj);
jukebox.Add(name, obj);
} // AddClip()
/*
Play a named audio clip.
Does not restart the clip if it is played twice in a row.
Will stop a previously playing clip to play this new clip.
*/
public void PlayClip(string name)
{
if (name == current_clip)
{
return;
} // if
if (current_clip != null)
{
StopClip();
} // if
((GameObject)jukebox[name]).audio.Play();
current_clip = name;
} // PlayClip()
public void StopClip()
{
((GameObject)jukebox[current_clip]).audio.Stop();
} // StopClip()
}
Answer by Eric5h5 · Mar 31, 2010 at 05:20 AM
DontDestroyOnLoad won't work unless the class extends MonoBehaviour. The code at the top (JukeboxController jukebox = etc.) can't be "bare" like that and needs to be inside a function, like Start.
Answer by qJake · Mar 29, 2010 at 05:56 PM
Haha... you definitely don't need the line in there that says "Usage:", and reading the error message specifically would have pointed you to that exact line of code. Just another reason you shouldn't mindlessly copy and paste code. ;)
a simple answere would have been nice.. So i got rid of that I still get an error. So what now.
I gave you a simple answer, and at the time, your post wasn't properly formatted, so I couldn't see what was going on. The first four lines of your script need to go somewhere else in your game, that's why it was labeled as "Usage", is because that's how you use it. Don't expect anything to work that you just copy and paste and don't take the time to figure out. Did the place where you got this code from explain how to use it, specifically?
Not exactly. I have gotten this from unifycommunity.com but they said it is ment to be used in the main game controller script. I will be honest I am not very good at c# but java i am.
Your answer
Follow this Question
Related Questions
unity 3d - c# scripting problems 1 Answer
Errors CS1525 and CS8025.... 1 Answer
Script Errors with C# 1 Answer
CS8025 Error (Parsing Error) 1 Answer
Parsing error with my code 3 Answers