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): using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Collections.Generic; [System.Serializable] public class MatLibDict { public string materialId = "name"; public Material mat; } public class MatLibController : MonoBehaviour { private MatLibController matlibmanager; public MatLibDict[] dictionary; void Start () { } void Update () { } } MatLib Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class MatLib : MonoBehaviour { public MatLibController manager; public string materialId; public bool singleMaterial; public bool multiMaterial; public int materialElement; void Start () { Debug.Log ("-1"); if (manager = null) { manager = FindObjectOfType (typeof(MatLibController)) as MatLibController; } Debug.Log ("0"); if (manager != null) { Debug.Log ("1"); if (GetComponent<MeshRenderer>() != null) { Debug.Log ("2"); foreach (MatLibDict mld in manager.dictionary) { Debug.Log ("3"); if (mld.materialId == materialId) { Debug.Log ("4"); if (singleMaterial) { GetComponent<MeshRenderer> ().material = mld.mat; } if (multiMaterial) { GetComponent<MeshRenderer> ().materials [materialElement] = mld.mat; } } } } } } } Anyone have any idea where I have gone wrong here?
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.