- Home /
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!!
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
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..
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)]);
}
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
Follow this Question
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