Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Mar 08, 2015 at 11:52 PM by KnightRiderGuy for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by KnightRiderGuy · Feb 23, 2015 at 06:16 AM · javascriptgameobjectdontdestroyonloadduplicatetimer-script

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

  • Sort: 
avatar image
0
Best Answer

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);
  }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fico_cofi · Apr 25, 2016 at 08:01 PM 0
Share

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 :)

avatar image
0

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.

?

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KnightRiderGuy · Feb 23, 2015 at 02:00 PM 0
Share

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

screen-shot-2015-02-23-at-75019-am.png (93.9 kB)
avatar image KnightRiderGuy · Feb 23, 2015 at 10:34 PM 0
Share

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);
 }

avatar image
0

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

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KnightRiderGuy · Feb 23, 2015 at 02:01 PM 0
Share

Thanks aloo-kr029 This I think would work well if I only had a few scenes but I have many scenes.

avatar image KnightRiderGuy · Feb 23, 2015 at 05:44 PM 0
Share

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. ;)

avatar image alok-kr-029 · Feb 24, 2015 at 04:13 AM 0
Share

if you have many scene then you can destroy the game object if its get duplicated by checking its count .

avatar image KnightRiderGuy · Feb 24, 2015 at 04:38 AM 0
Share

Yeah I mentioned that above, I fixed the duplication issue it's the other issue that's a bother now.

avatar image alok-kr-029 · Feb 24, 2015 at 04:43 AM 0
Share

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

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges