- Home /
Using multiple instances of the same script
I am making a game with multiple audio tracks that a player can turn on and off via hitting or missing certain colliders. There are 4 layers of audio tracks that I have working well but there is a script that needs to be applied to all layers yet only affect the sounds of the particular track it resides on.
What I have is a parent object called Layer1, Layer2, etc. that have an Audio Source on it. These have a child object of a collider that will set the audio playing if hit. At the moment every time I hit an object with this script, it starts the track again. I want it that if another object has been hit and set the track playing, then if this one gets hit it will ignore it. This script is as follows:
public class PlayNote : MonoBehaviour {
public GameObject sound;
void Start ()
{
sound = GameObject.Find(this.transform.parent.name.ToString());
sound.GetComponent("AudioSource");
}
void OnTriggerEnter(Collider other)
{
if(!AlreadyPlaying.isPlaying)
{
AlreadyPlaying.isPlaying = true;
sound.audio.Play();
this.gameObject.SetActive(false);
}
if(AlreadyPlaying.isPlaying)
{
this.gameObject.SetActive(false);
}
}
}
I have a child of this that also has a collider that when hit will stop the audio playing:
public class StopPlaying : MonoBehaviour {
public GameObject sound;
void Start()
{
sound = GameObject.Find(this.transform.parent.transform.parent.name.ToString());
sound.GetComponent("AudioSource");
}
void OnTriggerEnter(Collider other)
{
AlreadyPlaying.isPlaying = false;
sound.audio.Stop();
}
}
The last script is the one that I can't get to work with it all and this is the one that tells the other scripts weather or not the track is already playing and sits on the Layer1/Layer2 game objects that contain the sound:
public class AlreadyPlaying : MonoBehaviour {
public bool isPlaying;
void Start ()
{
isPlaying = false;
}
}
When it runs, the AlreadyPlaying script affects all instances of the bool, not the one specific to the gameobject that the script is sitting on. I have tried using GetComponent("AlreadyPlaying") but I cant seem to access the boolean on each specific layer.
Any help much appreciated.
Answer by Tomer-Barkan · Nov 11, 2013 at 02:47 PM
Check my answer in another post here, I think it's a very similar scenario to yours.
You need to create a script attached to your player, that turns on the audio source it collides with, and keeps a reference to the current playing source. Then, when it hits another audio source, you turn off the currently playing source (that's why you kept the reference), and turn on the new source, and also keep a reference to the new source.
Sorry for the late kudos, been flat out. That works for keeping the current track playing but I am after tracks to be able to be layered so I need the tracks to be independent of the player, and also that the track sits in a parent of the collidable object. I have managed to get what I was after by using a combination of transform.parent, GetComponent<"LayerName"> with some string manipulation to identify which layer each instance of the script is on.