- Home /
Object instantiation Logic Javascript.
Holla, i try to instantiate an object to the scene, which is the music background.. and i want this music to keep playing on the next scene
so first thing i have create the prefab.
and this is my script up till now..
#pragma strict
var bgSound : GameObject;
bgSound = GameObject.Find("bg_sound");
function Awake() {
if(GameObject.Find("bg_sound")==null){
Instantiate(bgSound,Vector3(0,0,0), Quaternion.identity);
DontDestroyOnLoad(bgSound);
}
else if(GameObject.Find("bg_sound(clone)")!=null){
//dont instantate new object..
}
}
but i dont know how to not instantiate new object, i know my logic seems off.. but i really think hard (lol) how to do this.. could someone please fix/give me some advice?
Answer by Kiloblargh · Dec 17, 2013 at 08:34 PM
Your main problem is that you put bgSound = GameObject.Find("bg_sound");
outside of a function, where it won't work.
You actually don't need to Instantiate it at all, you can just put the AudioSource on your main control script and then in Awake put DontDestroyOnLoad (this.gameObject)
. That will make your controller object persist, along with its music, from one scene to the next.
i';; try to put that into the function ! Thanks @$$anonymous$$iloblargh, emm for example i have 4 scene that used this bg_sound,
so i put the object on the first scene only? and if after i go to scene 5 and wnat to back to scene 4, isn't the object is now gone? because the sound triggered from the first scene object?