Search Unity

How to make a material look the same on different levels?

Discussion in 'Scripting' started by NirDafnai, Jul 26, 2014.

  1. NirDafnai

    NirDafnai

    Joined:
    Jul 18, 2014
    Posts:
    19
    Hey, so I used a script that I can change the material of my sphere on the main menu, but when I load the first level it doesn't work, it uses the default texture, I tried doing prefabs but that didn't work. any help?
    PM me for my teamviewer details.
     
  2. NirDafnai

    NirDafnai

    Joined:
    Jul 18, 2014
    Posts:
    19
  3. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    Then you need to call this script in Awake() or Start() so it sets the texture when the scene loads.
     
  4. NirDafnai

    NirDafnai

    Joined:
    Jul 18, 2014
    Posts:
    19
    This is the script that I attach to the buttons that if they are pressed they will change the material of my sphere
    Code (CSharp):
    1. using UnityEngine;
    2. public class SkinSelectorButton : MonoBehaviour {
    3.     // Reference to the sphere object.
    4.     public GameObject sphere;
    5.     // The material that is to be selected.
    6.     public Material skinMaterial;
    7.     private void OnMouseDown() {
    8.         sphere.renderer.sharedMaterial = skinMaterial;
    9.     }
    10. }
    what do I need to do?
     
  5. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    You could just apply the material you would like it to start with on the sphere in the editor.
     
  6. NirDafnai

    NirDafnai

    Joined:
    Jul 18, 2014
    Posts:
    19
    I did but it didn't work on the other sphere on the other level
     
  7. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
  8. NirDafnai

    NirDafnai

    Joined:
    Jul 18, 2014
    Posts:
    19
  9. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    Not even sure what teamviewer is, so No.

    There is another way to do it...
    Use a Settings script with a static material variable
    Ensure the script settings script uses DontDestroyOnLoad

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Settings : MonoBehaviour {
    5.  
    6.     static Material material;
    7.  
    8.     void Awake()
    9.     {
    10.         DontDestroyOnLoad(this.gameObject);
    11.     }
    12.  
    13. }
    Set the variable when you in the OnMouseDown method as well as the sphere.

    Code (CSharp):
    1. using UnityEngine;
    2. public class SkinSelectorButton : MonoBehaviour {
    3.     // Reference to the sphere object.
    4.     public GameObject sphere;
    5.     // The material that is to be selected.
    6.     public Material skinMaterial;
    7.     private void OnMouseDown() {
    8.         sphere.renderer.sharedMaterial = skinMaterial;
    9.         Settings.material = skinMaterial; // TADA
    10.     }
    11. }
    When the new scene is loaded have a Start() method set the material on the sphere from the settings script.

    Code (CSharp):
    1. //*** In a start method in the level
    2. void Start() {
    3.    sphere.renderer.sharedMaterial = Settings.material;
    4. }
    It's a bit more convoluted but should allow you to take the setting into the next level that is loaded and any level after that.
     
  10. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    You could also save the selection as a playerprefs, and load it on Start()
     
  11. NirDafnai

    NirDafnai

    Joined:
    Jul 18, 2014
    Posts:
    19
    Teamviewer is a program that you can remote control my computer so you could help me.
     
  12. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    Ah that could be useful if you're working with a small team you trust, but I personally would not want strangers I meet on a forum controlling my computer.

    But if you have any questions I will attempt to help.
     
  13. NirDafnai

    NirDafnai

    Joined:
    Jul 18, 2014
    Posts:
    19
    No I want you to control MY computer.
     
  14. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    No thank you, but I will try to help you with your problem on the forum.
     
  15. NirDafnai

    NirDafnai

    Joined:
    Jul 18, 2014
    Posts:
    19
    I don't know where to put all the script you sent me, what object? main camera?
     
  16. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    Create an empty game object, rename it to Settings and add the settings script (only needed in your material selection scene).

    Update your Button Script.

    Create a SetSkin script with the Start method and drop it on the sphere in your levels.
     
  17. NirDafnai

    NirDafnai

    Joined:
    Jul 18, 2014
    Posts:
    19
    What I need to update in my button script?
     
  18. NirDafnai

    NirDafnai

    Joined:
    Jul 18, 2014
    Posts:
    19
  19. NirDafnai

    NirDafnai

    Joined:
    Jul 18, 2014
    Posts:
    19
    It's really complicated the script that you wrote me and I don't know what to do... is there any way you could help me?
     
  20. NirDafnai

    NirDafnai

    Joined:
    Jul 18, 2014
    Posts:
    19
  21. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    That's the best advice I can provide, simple instructions and the code you need to do it. If anyone else can explain it better please do. If not can I suggest you try looking at the tutorials they might be able to help.