Search Unity

Object material save all ti

Discussion in 'Scripting' started by prosan11122, Oct 15, 2020.

  1. prosan11122

    prosan11122

    Joined:
    Feb 22, 2020
    Posts:
    8
    Hello! I'm somehow new in game developer, and I have a problem... I have a script that changes the material of an object,the script works but I want the last material that is on the object to save when I close the game, or when I change the scene. I couldn't find any posts to help me...Can you help me? Thanks!
    Code (CSharp):
    1. using System.Collections;
    2.  
    3. using System.Collections.Generic;
    4.  
    5. using UnityEngine;
    6.  
    7.  
    8.  
    9. public class ChangePlayerColor : MonoBehaviour
    10.  
    11. {
    12.  
    13.     public Material[] material;
    14.  
    15.     Renderer rend;
    16.  
    17.  
    18.  
    19.     private void Start()
    20.  
    21.     {
    22.  
    23.         rend = GetComponent<Renderer>();
    24.  
    25.         rend.enabled = true;
    26.  
    27.         rend.sharedMaterial = material[0];
    28.  
    29.     }
    30.  
    31.  
    32.  
    33.     public void Update()
    34.  
    35.     {
    36.  
    37.  
    38.  
    39.     }
    40.  
    41.  
    42.  
    43.     public void Blue()
    44.  
    45.     {
    46.  
    47.         rend.sharedMaterial = material[1];
    48.  
    49.     }
    50.  
    51.  
    52.  
    53.     public void Yellow()
    54.  
    55.     {
    56.  
    57.         rend.sharedMaterial = material[2];
    58.  
    59.     }
    60.  
    61.  
    62.  
    63.     public void Green()
    64.  
    65.     {
    66.  
    67.         rend.sharedMaterial = material[3];
    68.  
    69.     }
    70.  
    71.  
    72.  
    73.     public void Purple()
    74.  
    75.     {
    76.  
    77.         rend.sharedMaterial = material[4];
    78.  
    79.     }
    80.  
    81.  
    82.  
    83.     public void Roz()
    84.  
    85.     {
    86.  
    87.         rend.sharedMaterial = material[5];
    88.  
    89.     }
    90.  
    91.  
    92.  
    93.      public void Aqua()
    94.  
    95.     {
    96.  
    97.         rend.sharedMaterial = material[6];
    98.  
    99.     }
    100.  
    101.  
    102.  
    103.     public void Red()
    104.  
    105.     {
    106.  
    107.         rend.sharedMaterial = material[0];
    108.  
    109.     }
    110.  
    111.  
    112.  
    113.     public void FadeBlue()
    114.  
    115.     {
    116.  
    117.         rend.sharedMaterial = material[7];
    118.  
    119.     }
    120.  
     
  2. VishwasGagrani

    VishwasGagrani

    Joined:
    May 12, 2018
    Posts:
    84
    Joe-Censored likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Adding to Vish's post above, you can't save the actual material, but you would save the index of it.

    Here's an example of simple persistent loading/saving values using PlayerPrefs:

    https://pastebin.com/icJSq5zC

    Useful for a relatively small number of simple values.
     
    Joe-Censored likes this.
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Kurt-Dekker likes this.