Search Unity

C# Saving a prefab and the material

Discussion in 'Scripting' started by DeeJayVee, Mar 1, 2015.

  1. DeeJayVee

    DeeJayVee

    Joined:
    Jan 2, 2015
    Posts:
    121
    Hey guys, I have a script that allows me to change the color of the head and body of my player object by using sliders, everything works well and good except that when I press create, it creates a prefab of said object, however, the material does not save with it, thus the objects are rendered pink.

    I have tried

    Code (CSharp):
    1. GameObject.renderer.SharedMaterial.Color = new color(Red, Green,Blue);
    2.  
    That works with saving the material, however it makes both the head and the body share the same material, which defeats my purpose. Could anyone shed some light, please?


    Current Code;

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using UnityEngine.EventSystems;
    5. using UnityEditor;
    6. public class CharacterCreation : MonoBehaviour {
    7.     float Red = 0.9f;
    8.     float Green = 0.5f;
    9.     float Blue = 0.2f;
    10.  
    11.  
    12.  
    13.     float scrhgt = Screen.height;
    14.     float scrwdth = Screen.width;
    15.  
    16.  
    17.     public GameObject ChosenBodyPart = null;
    18.     public GameObject pHead, pBody = null;
    19.  
    20.     Color partsColor;
    21.  
    22.  
    23.  
    24.     public string PlayerName;
    25.     // Use this for initialization
    26.     void Start () {
    27.  
    28.         Red = (Random.Range(0.0f, 1.0f));
    29.         Green = (Random.Range(0.0f, 1.0f));
    30.         Blue = (Random.Range(0.0f, 1.0f));
    31.         ChosenBodyPart = GameObject.Find ("CubeHead");
    32.         pHead = GameObject.Find ("CubeHead");
    33.         pBody = GameObject.Find ("CubeBody");
    34.  
    35.  
    36.     }
    37.    
    38.     // Update is called once per frame
    39.     void Update () {
    40.         partsColor = new Color (Red, Green, Blue);
    41.         if (ChosenBodyPart != null) {
    42.             if(ChosenBodyPart == GameObject.Find ("CubeHead")){
    43.                 pHead.renderer.material.color = partsColor;
    44.             }else if(ChosenBodyPart == GameObject.Find("CubeBody")){
    45.                 pBody.renderer.material.color = partsColor;
    46.             }
    47.         }
    48.     }
    49.  
    50.  
    51.     void OnGUI(){
    52.         GUI.BeginGroup (new Rect (0, 0, scrwdth / 4, scrhgt));
    53.         GUI.Label (new Rect(10, 30, 80, 20), "Name : ");
    54.         PlayerName = GUI.TextField (new Rect (60, 30, 100, 20), PlayerName,25);
    55.         GUI.Box (new Rect (0, 0, scrwdth / 4, scrhgt), "Character Customization");
    56.  
    57.         //Body Colors
    58.         GUI.Label (new Rect (10, 80, 80, 30), "Coloring");
    59.         GUI.Label (new Rect (10, 120, 30, 20), "Red");
    60.         Red = GUI.HorizontalSlider (new Rect (10, 140, 100, 10), Red, 0f, 1f);
    61.         GUI.Label (new Rect (10, 160, 40, 20), "Green");
    62.         Green = GUI.HorizontalSlider (new Rect (10, 180, 100, 10), Green, 0f, 1f);
    63.         GUI.Label (new Rect (10, 200, 30, 20), "Blue");
    64.         Blue = GUI.HorizontalSlider (new Rect (10, 220, 100, 10), Blue, 0f, 1f);
    65.  
    66.         //####################################//
    67.  
    68.  
    69.              if (GUI.Button (new Rect (140, 130, 40, 40), "Head")) {
    70.             FocusedBodyPart ("CubeHead");
    71.         }
    72.  
    73.         if (GUI.Button (new Rect (140, 190, 40, 40), "Body")) {
    74.             FocusedBodyPart ("CubeBody");
    75.         }
    76.  
    77.  
    78.         if (GUI.Button (new Rect (140, 240, 80, 40), "Create")) {
    79.             GameObject player = GameObject.FindGameObjectWithTag("Player");
    80.             string name = player.name;
    81.             Object prefab = PrefabUtility.CreateEmptyPrefab("Assets/" + name + ".prefab");
    82.             PrefabUtility.ReplacePrefab(player, prefab);
    83.             AssetDatabase.Refresh();
    84.        
    85.         }
    86.  
    87.         if (GUI.Button (new Rect (10, 240, 100, 40), "Random")) {
    88.             Red = (Random.Range(0.0f, 1.0f));
    89.             Green = (Random.Range(0.0f, 1.0f));
    90.             Blue = (Random.Range(0.0f, 1.0f));
    91.         }
    92.  
    93.  
    94.         GUI.EndGroup ();
    95.     }
    96.  
    97.  
    98.     void FocusedBodyPart(string focusedbodypart){
    99.         if (focusedbodypart == "CubeHead") {
    100.         ChosenBodyPart = GameObject.Find ("CubeHead");
    101.  
    102.         } else if (focusedbodypart == "CubeBody") {
    103.             ChosenBodyPart = GameObject.Find ("CubeBody");
    104.         }
    105.     }
    106. }
    107.  
     
    Last edited: Mar 2, 2015
  2. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    I am not sure if this would work but trying won't hurt, so here goes nothing:
    Code (CSharp):
    1. public GameObject head;
    2. public Color headColor;
    3.  
    4. void Update()
    5. {
    6. //here we say that you slide the colors and set one
    7. headColor = theColorChosen;
    8.  
    9. //now we'll define the color to be set at "head" variable
    10. head.material.renderer.color = headColor;
    11. }
     
  3. DeeJayVee

    DeeJayVee

    Joined:
    Jan 2, 2015
    Posts:
    121
    I've tried to implement that into my code, but it doesn't make a change, I will update my first post with my code.
     
  4. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    244
    Sykoo is on the right track but has it backwards
    Code (CSharp):
    1. head.renderer.material.color
     
  5. DeeJayVee

    DeeJayVee

    Joined:
    Jan 2, 2015
    Posts:
    121
    Hey, Thanks for the reply, I have implemented that in the Update function, and it still doesn't save the material, the prefab is still saved pink.
     
  6. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    Oops, I'm sorry ^^ Thank you for correcting me :) This always has been a confusion for me :)
     
  7. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    Try saving the new color as a new texture instead then, as pink means "no texture found".