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 pigaroos · Feb 14, 2019 at 07:25 PM · colorscene-loadingbackgroundchanges

How to make a scene reload but with a different background color?

Hey! I have a trigger set up that reloads the scene, but I want it to reload with a different background color (vibrant, not too dark, hence the ColorHSV) Here's what I got:

 public Camera cam;
 
     public void LoadNextLevel()
     {
         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
         cam.backgroundColor = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);
     }

The scene changes color, but after a split second the scene reloads and the color goes back to normal. How do I fix this?

Thanks!!

Comment
Add comment · Show 2
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 LilGames · Feb 14, 2019 at 07:44 PM 1
Share

Your random color should be set within the scene, when it initializes.

avatar image pigaroos LilGames · Feb 14, 2019 at 08:15 PM 0
Share

@LilGames what do you mean, within the scene?

2 Replies

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

Answer by Rainsha · Feb 14, 2019 at 09:16 PM

Would it be an option for your background to set the random color on start by itself? Then the color would set up every time you load the scene. That means that your camera will have a script attached that on start will assign the random color.

However, if you want to persist the object though reloads (eg cause your first color cannot be random) you can set DontDestroyOnLoad on your camera. In this scenario, every time you reload the scene you will get the duplicate of the camera. A more robust way of doing this would be implementing the Singleton Pattern. This will cause you to keep the object with the component attached to it no matter the reloads and the would be duplicates will get destroyed.

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
1

Answer by Cornelis-de-Jager · Feb 14, 2019 at 09:25 PM

The idea behind the solution below is that you have an SceneLoader/GameManager object that doesn't get destroyed or reloaded between scenes. This allows you to call an action when the screen is loaded.

 public Camera cam;
 
 void Awake () {
     // Prevents this object to be destroyed on scene change
     DontDestroyOnLoad(this.gameObject);
 }
 
 // This will run on the first screen
 void Start () {
     // Initialize is called at the start of each load
     Initialize ();
     
     // Add the initialize function to the screen loader
     // Meaning it will run when a scene is loaded
     SceneManager.sceneLoaded += Initialize;
 }
 
 void Initialize () {
     cam = Camera.Main;
     cam.backgroundColor = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);
 }
 
 
 /* This just adds and removes the Initialize function when this object is disable or enabled
  * Only necessary when enabling or disable this script 
  */
 void OnEnable() {
     SceneManager.sceneLoaded += OnLevelFinishedLoading;
 }
 
 void OnDisable() {
     SceneManager.sceneLoaded -= OnLevelFinishedLoading;
 }
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

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

106 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

Related Questions

Change color of Gui.Box background to solid black 1 Answer

Unity Web Player backgroundcolor 1 Answer

A way to set a custom camera.viewport background color? 1 Answer

Material doesn't have a color property '_Color' 4 Answers

Specify background color of Gui.Button at runtime 1 Answer


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