Search Unity

Coded Material Libraries

Discussion in 'Scripting' started by Studio_Akiba, Aug 28, 2018.

  1. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    Haven't used Unity in quite a while due to other work obligations, but wanted to get back into my project by building a system to better manage my materials, seemed like a simple job in my head.

    The idea is to have a single managed in the scene which works like a dictionary and associates a name with a material, from another script placed on an object with a MeshRenderer you can provide it with a string and it will hunt for that string in the managers dictionary, it then grabs the corresponding material and assigns it to itself.

    I have debug lines EVERYWHERE in this yet I can't see where any of it is going wrong because the debugs don't seem to ping so either the code isn't running at all, or something is preventing it from debugging back.

    MatLibController
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. using System.Collections.Generic;
    6.  
    7. [System.Serializable]
    8. public class MatLibDict {
    9.     public string materialId = "name";
    10.     public Material mat;
    11. }
    12.  
    13. public class MatLibController : MonoBehaviour {
    14.  
    15.     private MatLibController matlibmanager;
    16.  
    17.     public MatLibDict[] dictionary;
    18.  
    19.     void Start () {
    20.        
    21.     }
    22.    
    23.     void Update () {
    24.        
    25.     }
    26. }
    27.  
    MatLib
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MatLib : MonoBehaviour {
    6.  
    7.     public MatLibController manager;
    8.  
    9.     public string materialId;
    10.  
    11.     public bool singleMaterial;
    12.     public bool multiMaterial;
    13.     public int materialElement;
    14.  
    15.     void Start () {
    16.         Debug.Log ("-1");
    17.         if (manager = null) {
    18.             manager = FindObjectOfType (typeof(MatLibController)) as MatLibController;
    19.         }
    20.         Debug.Log ("0");
    21.         if (manager != null) {
    22.             Debug.Log ("1");
    23.             if (GetComponent<MeshRenderer>() != null) {
    24.                 Debug.Log ("2");
    25.                 foreach (MatLibDict mld in manager.dictionary) {
    26.                     Debug.Log ("3");
    27.                     if (mld.materialId == materialId) {
    28.                         Debug.Log ("4");
    29.                         if (singleMaterial) {
    30.                             GetComponent<MeshRenderer> ().material = mld.mat;
    31.                         }
    32.                         if (multiMaterial) {
    33.                             GetComponent<MeshRenderer> ().materials [materialElement] = mld.mat;
    34.                         }
    35.                     }
    36.                 }
    37.             }
    38.         }
    39.     }
    40. }
    41.  
    Anyone have any idea where I have gone wrong here?
     
  2. recursive

    recursive

    Joined:
    Jul 12, 2012
    Posts:
    669
           if (manager = null) {
    is always assigning manager to null, you want to use ==.

    Some programmers will use
    null == manager
    to prevent this very issue, since
    null = manager
    will break at compile time.