- Home /
The question is answered, right answer was accepted
Java Script Dont Destroy On Load Duplicates Game Object
I have this don't destroy on load java script in order to make a timer keep time across scene changes, however when I return to my scene my timer game object has been duplicated!! Is there a way to fix that?
Answer by KnightRiderGuy · Feb 24, 2015 at 04:50 AM
On Unity Forums I found this piece of code that cured my duplication of my "CountdownTimerManager" game object:
But I still am having issues where my timer will no longer work when I go back into my main scene because the UI text is no longer connected to the script, you will have to see my screen-shot image attached in my post above to see how my Hierarchy is set up. I'm not even sure if this is even blasted well possible with the new UI system???
function Awake ()
{
// Make LevelData a singleton, so we keep ourself alive and kill any clones
if ( FindObjectsOfType(CountdownTimerManager).Length != 1 ) // If one of me already exists I must be a clone, so destroy me (don't feel bad about it)
Destroy (gameObject);
else // I'm the original, so keep me around
DontDestroyOnLoad (gameObject);
}
O$$anonymous$$ its good issue for this... But I have second problem... I need to kill new clone and my gameobject need to stay... $$anonymous$$y game object is text whos numbering coins, and when I restart level he need to continue his numbering, I do not know how can I make that... Help me please :)
Answer by Digital-Phantom · Feb 23, 2015 at 06:56 AM
Sounds like you have something in your scene that is Instantiating the timer. Do you have a game/scene manager object that you use to put the timer into a scene? If you do just remove it from all the scenes apart from the first one (or at least just remove the bit that tells the timer to appear)
You will only need it in the first scene as the same timer will be there due to the DontDestroyOnLoad command.
?
Thanks Digital Phantom, Not sure, this is how my scene is set up (See Attached), the lower circled object has the script on it and the panel has all of the buttons & timer text on it.
This is what the main part of the script is like, all of the rest is just buttons controls.
![#pragma strict
var Alarm : AudioClip ;
var timer: float = 3600;
var isFinishedLevel : boolean = true;
public var displayText : UnityEngine.UI.Text;
public var timeText : UnityEngine.UI.Text;
var $$anonymous$$sDisplay : String;
var secsDisplay : String;
var mySeconds : int = 0;
private var oldTimer : float;
//Begin New
//End New
/*function Awake () {
DontDestroyOnLoad (transform.gameObject);
}*/
function Start(){
oldTimer = timer;
}
function Update(){
if (!isFinishedLevel) {
timer -= Time.deltaTime;
}
CurrentTime();
}
function CurrentTime() {
var dt : System.DateTime = System.DateTime.Now;
var h : int = dt.Hour;
var m : int = dt.$$anonymous$$inute;
var s : int = dt.Second;
timeText.text = h + ":" + m + ":" + s;
if(mySeconds != s)
{
mySeconds = s;
Ti$$anonymous$$g();
}
}
function Ti$$anonymous$$g()
{
if (timer > 0) {
//var $$anonymous$$sDisplay : String = parseInt( timer / 60 ).ToString();
$$anonymous$$sDisplay = parseInt( timer / 60 ).ToString();
//var secsDisplay : String = parseInt( timer ).ToString();
secsDisplay = parseInt( timer ).ToString();
if ( (timer - ( parseInt($$anonymous$$sDisplay) * 60)) > 10 ) {
secsDisplay = parseInt( timer - ( parseInt($$anonymous$$sDisplay) * 60) ).ToString();
}
else {
secsDisplay = "0" + parseInt( timer - ( parseInt($$anonymous$$sDisplay) * 60) ).ToString();
}
//displayText.text = $$anonymous$$sDisplay + " : " + secsDisplay;
}
//Timer Reaches End We Can Do Something Here
else {
timer += oldTimer;
audio.PlayOneShot(Alarm);//Plays Alarm Sound
isFinishedLevel = true;//Sets Inspector Value to true or false based on what is set here
yield WaitForSeconds (0.8);//Wait Time Setting
//Do Something if Desired
Debug.Log ("Timer Ended");
}
displayText.text = $$anonymous$$sDisplay + " : " + secsDisplay;
}][1]
[1]: /storage/temp/41238-screen-shot-2015-02-23-at-75019-am.png
On Unity Forums I found this piece of code that cured my duplication of my "CountdownTimer$$anonymous$$anager" game object:
But I still am having issues where my timer will no longer work when I go back into my main scene because the UI text is no longer connected to the script, you will have to see my screen-shot image attached in my post above to see how my Hierarchy is set up. I'm not even sure if this is even blasted well possible with the new UI system???
function Awake ()
{
// $$anonymous$$ake LevelData a singleton, so we keep ourself alive and kill any clones
if ( FindObjectsOfType(CountdownTimer$$anonymous$$anager).Length != 1 ) // If one of me already exists I must be a clone, so destroy me (don't feel bad about it)
Destroy (gameObject);
else // I'm the original, so keep me around
DontDestroyOnLoad (gameObject);
}
Answer by alok-kr-029 · Feb 23, 2015 at 07:04 AM
you can do it by checking which level you want to implement the dontdestryonload eg:
void Start()
{
if(Application.loadedLevelName != "Your Level Name which you dont want to duplicate");
DontDestroyOnLoad ("your item");
}
Its in C# change it to Jscript
Thanks aloo-kr029 This I think would work well if I only had a few scenes but I have many scenes.
I'm not good at converting C# to Java would you be so kind? I'll give this one a shot and see if it has any effect on the way I have this set up. ;)
if you have many scene then you can destroy the game object if its get duplicated by checking its count .
Yeah I mentioned that above, I fixed the duplication issue it's the other issue that's a bother now.
Thats great you have solved the issue . if you have other issue than why dont you post it in answer as there are many developer who can suggest you some solution
Follow this Question
Related Questions
DontDestroyOnLoad duplication. 0 Answers
SendMessage on DontDestroyOnLoad object 1 Answer
DontDestory gameobject doesn't work on button click after a new scene loads 2 Answers
static var for one object. 2 Answers
"GameObject.Find();" not working 2 Answers