Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 jakethomashall · Aug 23, 2018 at 11:17 PM · scripting problemscenebeginnerscene-loading

How do you deactivate a static script until a certain key is pressed?

Hey there, I have a problem in that the game I'm making has a "start screen", which basically disables game objects until a certain key is pressed, hence starting the game. The code to do this looks like so:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 
 public class LoadScreen : MonoBehaviour {
 
     public GameObject startScreen;
     public GameObject player;
     public GameObject gameOverManager;
     public GameObject blockSpawner;
 
     bool waitingToStartGame = true;
 
 
     void Start () {
         if (startScreen != null)
         {
             startScreen.SetActive(true);
         }
         else
         {
             waitingToStartGame = false;
             Debug.LogError("Start screen has not been set in the inspector, please assign the start screen UI and try again.");
         }
         if (player != null)
         {
             player.SetActive(false);
         }
         else
         {
             Debug.LogError("Player has not been set in the inspector, please assign the start screen UI and try again.");
         }
         if (gameOverManager != null)
         {
             gameOverManager.SetActive(false);
         }
         else
         {
             Debug.LogError("Game over manager has not been set in the inspector, please assign the start screen UI and try again.");
         }
         if (blockSpawner != null)
         {
             blockSpawner.SetActive(false);
         }
         else
         {
             Debug.LogError("blockspawner has not been set in the inspector, please assign the start screen UI and try again.");
         }
     }
     
     
     void Update () {
         if (waitingToStartGame && (Input.GetKeyDown(KeyCode.Space)))
         {
             waitingToStartGame = false;
             if (startScreen != null)
             {
                 startScreen.SetActive(false);
             }
             if (player != null)
             {
                 player.SetActive(true);
             }
             if (gameOverManager != null)
             {
                 gameOverManager.SetActive(true);
             }
             if (blockSpawner != null)
             {
                 blockSpawner.SetActive(true);
             }
         }
     }
 }


The problem is that I have a static piece of code that is acting as a difficulty control, it has a function that is recording the time passed since the scene is loaded, the problem is that it starts counting up as soon as the game loads, meaning when the player actually starts the game by pressing "space", the difficulty has already incremented by (x) amount.

Here is the code from the static class:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public static class Difficulty {
 
     static float secondsToMaxDiff = 90;
 
     public static float GetDifficultyPercent() {
         //return 1;
         return Mathf.Clamp01(Time.timeSinceLevelLoad / secondsToMaxDiff);
     }
 
 }

How can I go about fixing this? Will I need to create a seperate scene that contains the "start screen", and then once space is pressed it loads the actual game? Or is there a way to make the current setup work?

Thank you in advance for any help!

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
0
Best Answer

Answer by jakethomashall · Aug 23, 2018 at 11:34 PM

Figured it out, for future reference; turns out that using another scene is the easiest method, just create another scene then under File > Build Settings drag the new scene into the "scenes to build". Drag the scene to the top of the order.

To load the scene simply create a script that runs on an empty game object, use an if within the update function to control when it gets activated. Once the if statement is met use this code:

 SceneManager.LoadScene(1);

And then the actual game will be loaded, you can also reference the scene name directly instead of calling it by its order on the "scenes to build" page if you fancy.

Comment
Add comment · 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
0

Answer by JVene · Aug 23, 2018 at 11:41 PM

What you need is a 'vector' in time. Time.timeSinceLevelLoad would be correct if the game started running right away. It doesn't. It starts later. When? Well, at that time when you hit space and start the game.


So, create a static float that records the Time.timeSinceLevelLoad, say something like:

 timeLaunched = Time.timeSinceLeveLLoad;

in that code where the space key initiates the game.


Now, your formulae involves the difference, something like:

  public static float GetDifficultyPercent() 
     {
          return Mathf.Clamp01( ( ( Time.timeSinceLevelLoad - timeLaunched )  / secondsToMaxDiff);
     }

 




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 jakethomashall · Aug 23, 2018 at 11:43 PM 0
Share

Thanks for the reply, that would have worked as well, I found a different solution in the end though. (just used a different scene)

I rewarded a point to you, whatever that does lol.

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

177 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 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 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 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 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 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 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 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 avatar image

Related Questions

bool doesn't change?? 0 Answers

Bolt Visual scripting and loading different scenes/levels 1 Answer

how i save that in my data in load or save 2 Answers

"Scene couldn't be loaded because it isn't added to the build settings" but it is? 1 Answer

How do I preload multiple scenes at once? 0 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