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 Borr1310 · Oct 20, 2016 at 02:44 PM · scenerandombackgroundifif statement

Something that only applies to current scene

I have a very simple question... I have 2 scenes, main menu and game scene. I have a background and there is a random generator script attached to it.

 if ()
         {
             _renderer.material.SetTexture("_MainTex", _textures[Random.Range(0, _textures.Length)]);
         }

the problem I am having is that I want to randomly change the background only on main menu scene and than keep the same background on game scene. The solution is very simple I guess I should add na if statement

if(currentscene´s name == "lala") do blahblah

What could I add into the if statement.. something that only applies to the current scene?

EDIT: The problem is different.

 if (SceneManager.GetActiveScene().name == "MainMenu")
         {
             _renderer.material.SetTexture("_MainTex", _textures[Random.Range(0, _textures.Length)]);
         }
         else
         {
 
         }

It kinda works, it generates a random BG when I start the game but then in the play scene It doesn´t load the "current BG" but it loads the default one, how would I fix that?

EDIT 2:

I don´t want to create a new question for that.. _ _renderer.material.SetTexture(...) does not actualy apply the texture. The problem is -I want to randomly change the texture only in MainMenu scene and if I do, it doesnt load that texture in the the GameScene it loads the one thats litterarly attached to it. What do I do??

Thanks again.

Thanks!!

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 Zodiarc · Oct 20, 2016 at 02:49 PM

If you want to keep things between scenes use DontDestroyOnLoad()

https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html

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 Borr1310 · Oct 20, 2016 at 03:03 PM 0
Share

It kinda works. When I press play the background is the same but then it never changes even after I die and the play scene restarts..

avatar image
0

Answer by Veroxi · Oct 20, 2016 at 03:49 PM

I think you might need to use Scene.name. Your code would look something like:

  if (Scene.name == "yourSceneName")
          {
              _renderer.material.SetTexture("_MainTex", _textures[Random.Range(0, _textures.Length)]);
          }


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 Borr1310 · Oct 20, 2016 at 04:05 PM 0
Share

An object reference is required for the non-static field, method, or property 'Scene.name'
if (Scene.name == "$$anonymous$$ain$$anonymous$$enu") { renderer.material.SetTexture("$$anonymous$$ainTex", _textures[Random.Range(0, _textures.Length)]); }

Cant get it to work even if I write Scene. there is no name on the list.. ?

Edit: ID$$anonymous$$ if I am thiniking right but I have to get the active scene at runtime so I also tried

 Scene$$anonymous$$anager.GetActiveScene().name == "$$anonymous$$ain$$anonymous$$enu"

It doesn´t work so could the problem be that I am calling the function in Start()?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Random value for if statement 2 Answers

Random level select 1 Answer

Choose random background at start 1 Answer

Editor scene preview window 0 Answers

What does if (someComponent) statement return? Where would anyone use it? 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