Any ideas as to why I get this error? (CS1061)
Hi unity,
Im getting this error message: error cs1061: Type 'unityEngine.Component' does not contain a definition for materials does anyone know why i get this error?
this is my code:
 using UnityEngine;
 
 [AddComponentMenu("Rendering/SetRenderQueue")]
 public class SetRenderQueue : MonoBehaviour {
 
     [SerializeField]
     protected int[] m_queues = new int[]{3000};
     
     protected void Awake(){
         Material[] materials = renderer.materials;
         for (int i = 0; i < materials.Length && i < m_queues.Length; ++i){
             materials[i].renderQueue = m_queues[i];
         }
     }
 }
 
              Answer by HenryStrattonFW · Feb 10, 2017 at 09:27 PM
This is because you are accessing the renderer via the deprecated property "renderer" this property is now of type "Component" not of type "Renderer" This was done alongside a load of other similar properties like rigidbody to reduce coupling in the unity code base and make things a bit more modular.
What you need to do is either cast renderer to an appropriate type, or better yet, store an appropriate reference to it as a variable using GetComponent. for example. (Code done in notepad, may be slight syntax errors).
 using UnityEngine;
  
  [AddComponentMenu("Rendering/SetRenderQueue")]
  public class SetRenderQueue : MonoBehaviour {
  
      [SerializeField]
      protected int[] m_queues = new int[]{3000};
      
      private MeshRenderer m_renderer;
      
      protected void Awake(){
        m_renderer = GetComponent<MeshRenderer>();
          Material[] materials = m_renderer.materials;
          for (int i = 0; i < materials.Length && i < m_queues.Length; ++i){
              materials[i].renderQueue = m_queues[i];
          }
      }
  }
 
              Your answer
 
             Follow this Question
Related Questions
Error stage @username 1 Answer
Can't add Orbital script - "The script needs to derive from MonoBehaviour!" 0 Answers
Create a copy of a non MonoBehaviour class and set variables from other class 0 Answers
ArgumentOutOfRange: Argument is out of range Parameter name: index 0 Answers
I have an error on a C# script @username 2 Answers