Search Unity

Spawning a game object prefab gives a black color material by default, instead of the material given

Discussion in 'Scripting' started by asperatology, Aug 8, 2015.

  1. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    This is the code I have just for spawning in a game object prefab:

    Code (CSharp):
    1. GameObject duplicate = GameObject.Instantiate<GameObject>(this.tutorialUnitPrefab);
    2. duplicate.transform.position = owner.transform.position;
    And this is the default material I have set for the game object prefab. The color of the default material is white:



    And this is how it shows up at the start of the game:



    As soon as I spawn in a clone of the game object you saw in the playing field, the spawned clone looks like this:



    The Albedo value is set to 0, or RGB(0, 0, 0), or black.

    Does anyone know what I'm doing wrong here?

    Full class code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. namespace Tutorial {
    6.     public class TutorialInputManager : MonoBehaviour {
    7.  
    8.         public List<GameObject> selectedObjects;
    9.         public List<GameObject> allObjects;
    10.         public List<GameObject> boxSelectedObjects;
    11.  
    12.         public bool selectionTutorialFlag;
    13.         public bool attackOrderTutorialFlag;
    14.         public bool moveOrderTutorialFlag;
    15.         public bool splitTutorialFlag;
    16.  
    17.         public bool attackStandingByFlag;
    18.         public GameObject tutorialUnitPrefab;
    19.  
    20.         public TutorialAttackManager attackManager;
    21.         public TutorialSplitManager splitManager;
    22.  
    23.  
    24.         //----------------------------------
    25.  
    26.  
    27.         void Start() {
    28.             this.selectedObjects = new List<GameObject>();
    29.             this.allObjects = new List<GameObject>();
    30.             this.boxSelectedObjects = new List<GameObject>();
    31.  
    32.             this.selectionTutorialFlag = this.attackOrderTutorialFlag = this.moveOrderTutorialFlag = this.splitTutorialFlag = true;
    33.  
    34.             if (this.attackManager == null) {
    35.                 Debug.LogError("Cannot find attack manager for the tutorial.");
    36.             }
    37.             if (this.splitManager == null) {
    38.                 Debug.LogError("Cannot find split manager for the tutorial.");
    39.             }
    40.  
    41.             GameObject[] existingObjects = GameObject.FindGameObjectsWithTag("Tutorial_Unit");
    42.             foreach (GameObject obj in existingObjects) {
    43.                 this.allObjects.Add(obj);
    44.             }
    45.         }
    46.  
    47.         void Update() {
    48.             SelectOrder();
    49.             AttackOrder();
    50.             MoveOrder();
    51.             SplitOrder();
    52.  
    53.             UpdateStatus();
    54.         }
    55.  
    56.         //----------------------------------
    57.  
    58.         private void UpdateStatus() {
    59.             if (this.attackStandingByFlag) {
    60.                 foreach (GameObject obj in this.allObjects) {
    61.                     TutorialSelectable select = obj.GetComponent<TutorialSelectable>();
    62.                     if (this.selectedObjects.Contains(obj)) {
    63.                         select.SetAttackStandby();
    64.                     }
    65.                     else {
    66.                         select.SetDeselect();
    67.                     }
    68.                 }
    69.             }
    70.             else {
    71.                 foreach (GameObject obj in this.allObjects) {
    72.                     TutorialSelectable select = obj.GetComponent<TutorialSelectable>();
    73.                     if (this.selectedObjects.Contains(obj) || this.boxSelectedObjects.Contains(obj)) {
    74.                         select.SetSelect();
    75.                     }
    76.                     else {
    77.                         select.SetDeselect();
    78.                         select.SetAttackCancel();
    79.                     }
    80.                 }
    81.             }
    82.         }
    83.  
    84.         //----------------------------------
    85.  
    86.         private void SelectOrder() {
    87.             if (!this.selectionTutorialFlag) {
    88.                 return;
    89.             }
    90.             if (this.attackStandingByFlag) {
    91.                 return;
    92.             }
    93.             if (Input.GetMouseButtonDown(0)) {
    94.                 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    95.                 RaycastHit[] hits = Physics.RaycastAll(ray);
    96.                 bool hasHitUnit = false;
    97.                 foreach (RaycastHit hit in hits) {
    98.                     GameObject obj = hit.collider.gameObject;
    99.                     if (obj.tag.Equals("Tutorial_Unit")) {
    100.                         hasHitUnit = true;
    101.                         if (!this.selectedObjects.Contains(obj)) {
    102.                             this.selectedObjects.Add(obj);
    103.                         }
    104.                         break;
    105.                     }
    106.                 }
    107.                 if (!hasHitUnit) {
    108.                     this.selectedObjects.Clear();
    109.                 }
    110.             }
    111.             if (Input.GetMouseButton(0)) {
    112.                 foreach (GameObject obj in this.allObjects) {
    113.                     Vector2 screenPoint = Camera.main.WorldToScreenPoint(obj.transform.position);
    114.                     screenPoint.y = Screen.height - screenPoint.y;
    115.                     if (Selection.selectionArea.Contains(screenPoint) && !this.boxSelectedObjects.Contains(obj)) {
    116.                         this.boxSelectedObjects.Add(obj);
    117.                     }
    118.                     else if (!Selection.selectionArea.Contains(screenPoint) && this.boxSelectedObjects.Contains(obj)) {
    119.                         this.boxSelectedObjects.Remove(obj);
    120.                     }
    121.                 }
    122.             }
    123.             if (Input.GetMouseButtonUp(0)) {
    124.                 if (this.boxSelectedObjects.Count > 0) {
    125.                     foreach (GameObject obj in this.boxSelectedObjects) {
    126.                         if (!this.selectedObjects.Contains(obj)) {
    127.                             this.selectedObjects.Add(obj);
    128.                         }
    129.                     }
    130.                     this.boxSelectedObjects.Clear();
    131.                 }
    132.             }
    133.         }
    134.  
    135.         //----------------------------------
    136.  
    137.         private void AttackOrder() {
    138.             if (!this.attackOrderTutorialFlag) {
    139.                 return;
    140.             }
    141.             if (Input.GetKeyDown(KeyCode.A)) {
    142.                 if (!this.attackStandingByFlag) {
    143.                     if (this.selectedObjects.Count > 0) {
    144.                         this.attackStandingByFlag = true;
    145.                         foreach (GameObject obj in this.selectedObjects) {
    146.                             TutorialSelectable select = obj.GetComponent<TutorialSelectable>();
    147.                             select.SetAttackStandby();
    148.                         }
    149.                     }
    150.                 }
    151.             }
    152.             if (this.attackStandingByFlag) {
    153.                 if (Input.GetMouseButtonDown(0)) {
    154.                     this.attackStandingByFlag = false;
    155.                     this.selectedObjects.Clear();
    156.                 }
    157.                 else if (Input.GetMouseButtonDown(1)) {
    158.                     this.attackStandingByFlag = false;
    159.                     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    160.                     RaycastHit[] hits = Physics.RaycastAll(ray);
    161.                     foreach (RaycastHit hit in hits) {
    162.                         GameObject obj = hit.collider.gameObject;
    163.                         if (obj.name.Equals("Floor")) {
    164.                             AttackOrder order = new AttackOrder();
    165.                             order.Create(hit.point, this.selectedObjects);
    166.                             this.attackManager.attackOrders.Add(order);
    167.                             break;
    168.                         }
    169.                     }
    170.                 }
    171.             }
    172.         }
    173.  
    174.         //----------------------------------
    175.  
    176.         private void MoveOrder() {
    177.             if (!this.moveOrderTutorialFlag) {
    178.                 return;
    179.             }
    180.             if (!this.attackStandingByFlag) {
    181.                 if (this.selectedObjects.Count > 0) {
    182.                     if (Input.GetMouseButtonDown(1)) {
    183.                         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    184.                         RaycastHit[] hits = Physics.RaycastAll(ray);
    185.                         foreach (RaycastHit hit in hits) {
    186.                             GameObject obj = hit.collider.gameObject;
    187.                             if (obj.name.Equals("Floor")) {
    188.                                 foreach (GameObject select in this.selectedObjects) {
    189.                                     NavMeshAgent agent = select.GetComponent<NavMeshAgent>();
    190.                                     agent.SetDestination(hit.point);
    191.                                 }
    192.                                 break;
    193.                             }
    194.                         }
    195.                     }
    196.                 }
    197.             }
    198.         }
    199.  
    200.         //----------------------------------
    201.  
    202.         private void SplitOrder() {
    203.             if (!this.splitTutorialFlag) {
    204.                 return;
    205.             }
    206.             if (this.selectedObjects.Count > 0) {
    207.                 if (Input.GetKeyDown(KeyCode.S)) {
    208.                     foreach (GameObject owner in this.selectedObjects) {
    209.                         GameObject duplicate = GameObject.Instantiate<GameObject>(this.tutorialUnitPrefab);
    210.                         duplicate.transform.position = owner.transform.position;
    211.                         TutorialSelectable select = owner.GetComponent<TutorialSelectable>();
    212.                         select.DisableSelection();
    213.                         select = duplicate.GetComponent<TutorialSelectable>();
    214.                         select.DisableSelection();
    215.                         this.allObjects.Add(duplicate);
    216.                         this.splitManager.splitGroups.Add(new SplitGroup(owner, duplicate));
    217.                     }
    218.                     this.selectedObjects.Clear();
    219.                 }
    220.             }
    221.         }
    222.     }
    223. }
    224.  
     
  2. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    It seems the Alpha is set to 0. Unless you can show the material I can't really determine what's wrong with it.
     
  3. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Your material has an alpha value of 0, it can be determined by the line under the color itself, the longer the white line in it, the higher the alpha is, if there isn't a white line, then it's 0. And I think the material of the sphere is not transparent, so unity defaulted it to black.
     
  4. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    I just created a brand new material, and left it there. I didn't edit any settings. I really don't know how to show the material, other than showing it in the Inspector.

    This is the full view from the Inspector, which is the same as the second image in my first post:




    Even when I set the alpha value to 255, it still shows up as black. Full screen screenshot:

     
  5. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Can you please send a pic about the material tab, it's closed, maybe it has some problems.
     
  6. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    I'm not sure where the material tab is located. Do you mean the material for the black ball?



    And this is the while ball:

     
  7. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Okay, the lazy way of solving it:
    Code (CSharp):
    1. Put this code after the instantiation:
    2. duplicate.renderer.material.color = Color.White;
     
    asperatology likes this.
  8. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Interestingly, this fixed it.

    Just a note, GameObject.renderer no longer has "material" as a class member. I need to use GetComponent() to fetch "renderer" and work from there.

    Thanks, @gorbit99.
     
  9. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Yeah, I'm used to the old system, where you just write rigidbody.AddForce, and don't need to Get it as a component.