Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Change Material of GameObject in another Scene

Discussion in 'Scripting' started by elliotbra, Jul 9, 2023.

  1. elliotbra

    elliotbra

    Joined:
    Jul 21, 2022
    Posts:
    46
    Hi, I have a Main Menu and a gameplay scene in my game. In the main menu you can select a background for the gameplay. But how do I change the material of the background over different scenes?
     
  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    541
    Your main menu saves the setting to a file or PlayerPrefs and then when the scene loads in it loads in the setting and sets the background.
     
    Last edited: Jul 9, 2023
  3. elliotbra

    elliotbra

    Joined:
    Jul 21, 2022
    Posts:
    46
    But how do I save this as a playerprefs?
     
  4. VengarlofForossa

    VengarlofForossa

    Joined:
    Jan 1, 2023
    Posts:
    18
    It can have a function with different colours. The 1 is yellow, the 2 is red, the 3 is green...
    The player chooses the 3 and in a PlayerPref you save the 3 and in the next scene by code you say that it is the 3 to take.

    --------------------------------------------

    You can also use DontDestroyOnLoad, so that when you change scene that piece of code will survive for the next scene.

    https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
     
    zulo3d likes this.