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 /
avatar image
0
Question by MakeITeasy · Dec 06, 2013 at 03:57 PM · javascriptsceneupdate

Problem with restarting the scene (js)

Hello everyone!

When the 1st scene starts the timer capsule object starts to shrink while time is running out and when time ends a game over texture appears on the screen and then u go back to main menu. When you click on play the 1st scene loads again but the timer capsule object is still shrank (scale.y=0). I tried to set again my object with the default scale and color on Start() but it doesn't work. :(

How do i set back the capsule object to its first size everytime i load the scene?

The js script called Begin is attached to MainCamera object and it is shown below:

 public var capsuleTimer: GameObject;
 public static var flagTxtr:boolean = false;
 public static var endTimer:int =0;
 public var gameOverClip: AudioClip;
 public var hasPlayed:boolean=false;
 public var disable_cTmr:boolean=false;
 
 function Update(){
 
 if(disable_cTmr == false){
     var cTmr= capsuleTimer.Find("capsuleTimer");
     cTmr.renderer.material.color = Color( 0/255.0, 205/255.0, 102/255.0, 0);
     var timer:int = 200;
     var timer_:int;
     var TimeSpeed = 50;
 
     timer_ = timer - (Time.time * TimeSpeed);
     cTmr.transform.localScale.y = Mathf.Clamp(timer_, 0, 200);
     endTimer = timer_;
     var light_ = oDLight.Find("Directional light_1");
 
     if(timer_ <= 100)
         cTmr.renderer.material.color = Color( 255/255.0, 165/255.0, 0/255.0, 0);
     if(timer_ <= 50)
         cTmr.renderer.material.color = Color( 238/255.0, 0/255.0, 0/255.0, 0);
     if(timer_ == 0){
         flagTxtr=true; // make gameover texture visible by setting variable flagTxtr to true
         if(hasPlayed == false){
             //Debug.Log("Game Over!");
             audio.PlayOneShot(gameOverClip); 
             hasPlayed = true;
         }    
         light_.light.enabled = false;
         Invoke("LoadGameOver",10);
     }
 }
     
 }
 
 function LoadGameOver(){ //go to High score Scene (from there u can move to main menu and then click on play where my game scene loads
         Application.LoadLevel ("High_Score");  
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Spinnernicholas · Dec 06, 2013 at 04:36 PM

You can just replace Time.time with Time.timeSinceLevelLoad. That is the time since the last level(scene) was loaded.

 //........
 timer_ = timer - (Time.timeSinceLevelLoad * TimeSpeed);
 //........

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 deltamish · Dec 06, 2013 at 05:18 PM 0
Share

Nice i didnt know we had that thanks

avatar image MakeITeasy · Dec 06, 2013 at 05:30 PM 0
Share

Great! Nether do i... So many surprises that Unity3d lol Thanks so much for your help @Spinnernicholas

avatar image
0

Answer by deltamish · Dec 06, 2013 at 04:21 PM

The Reason is Time.time is number seconds passed since start of the game it doesnt change while switching scenes

So to fix it add these codes

 var StartTime:float =0;
 
 function Start(){
 StartTime = Time.time;
 }
 
 function Update(){
 
 //your code
  timer_ = timer - (StartTime * TimeSpeed);
 
 //rest of code
 }
 
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 MakeITeasy · Dec 06, 2013 at 05:26 PM 0
Share

@deltamish It's not working.

I think on Start() variable StartTime gets value 0 and on Update() it doesn't start to count at all but stays as 0. That means i can't see my capsule object (timer) Shrinks each time.

Thanx for ur time, anyway.

avatar image deltamish · Dec 06, 2013 at 07:17 PM 0
Share

soory abt that normally this will work for timers

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 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

Related Questions

How to find a prefab in another scene? 1 Answer

Javascript Class Update 2 Answers

How do I get a function to recognize a variable in function Update() 1 Answer

Understanding The Undo Function (JavaScript) 2 Answers

Unity error 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