- Home /
This post has been wikified, any user with enough reputation can edit it.
Question by
jmparavicini · Oct 10, 2014 at 11:02 PM ·
instantiatedontdestroyonloadduplicate
DontDestroyOnLoad duplicate solve test failed.
Hello everyone
I have 2 script and im trying that the dontdestroyonload doesnt duplicate the objects when i come back to the first scene i tried to count the tag of the object and if its zero the it should instantiate a prefab but i didnt work here are my scripts
The duplicate solver: #pragma strict
var audio2 : GameObject;
var clonedObject : Transform;
var audioCounter : int;
function Update ()
{
audioCounter = GameObject.FindGameObjectsWithTag("audio");
if(audioCounter == 1)
{
Instantiate(audio2, clonedObject.transform.position, transform.rotation);
}
}
and the script with the dontdestroyonload
#pragma strict
function Start () {
}
function Update ()
{
DontDestroyOnLoad(this);
if(Application.loadedLevel <= 1)
{
audio.volume = MenuMastering.myVolume;
}
if(Application.loadedLevel == 2)
{
audio.volume = 0;
}
}
if anyone can tell me how i can make the gameobject with the second script not to be duplictaed it would be very apreciatet and if someone posts a singleton pattern link or script please let it be in java i dont know c#.
thanks in advance skullbeats1
Comment
Answer by AmmarAlsharekh · Jan 09, 2015 at 07:21 AM
public static bool IsPlay = false;
// Use this for initialization
public string KillOnLevel;
void Start()
{
if (!IsPlay)
{
DontDestroyOnLoad(this.gameObject);
IsPlay = true;
audio.Play();
}
else
{
Destroy(this);
}
}
// Update is called once per frame
void Update()
{
//Debug.Log("Run");
if (Application.loadedLevelName == KillOnLevel)
{
audio.Stop();
IsPlay = false;
Destroy(this);
}
}