Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Where is the Renderer component ? It's not the in unity editor menu Component

Discussion in 'Scripting' started by Chocolade, Nov 28, 2016.

  1. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    916
    Code (csharp):
    1. tmpobj.name = "Plane"; tmpobj.GetComponent<Renderer>().material.color = new Color(0.9490f, 0.8901f, 0.7686f, 0);
    I need to add to the script a component called Renderer but i can't find it in the Component menu.
    Not in Component > Rendering and not in other places.

    The complete script:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class DragAndDrop : MonoBehaviour
    6. {
    7.  
    8.     void Start()
    9.     {
    10.         GameObject tmpobj = new GameObject(); MeshFilter mf = tmpobj.AddComponent<MeshFilter>();
    11.  
    12.         tmpobj.name = "Plane"; tmpobj.GetComponent<Renderer>().material.color = new Color(0.9490f, 0.8901f, 0.7686f, 0);
    13.  
    14.         Vector3[] verts = new Vector3[4]; Vector2[] uv = new Vector2[4]; Vector3[] normals = new Vector3[4]; int[] tri = new int[6];
    15.  
    16.         verts[0] = new Vector3(10f, 10f, 10f); verts[1] = new Vector3(10f, 10f, 10f); verts[2] = new Vector3(10f, 10f, 10f); verts[3] = new Vector3(10f, 10f, 10f);
    17.  
    18.         normals[0] = new Vector3(10f, 10f, 10f); normals[1] = new Vector3(10f, 10f, 10f); normals[2] = new Vector3(10f, 10f, 10f); normals[3] = new Vector3(10f, 10f, 10f);
    19.  
    20.         uv[0] = new Vector2(10f, 10f); uv[1] = new Vector2(10f, 10f); uv[2] = new Vector2(10f, 10f); uv[3] = new Vector2(10f, 10f);
    21.  
    22.         tri[0] = 2; tri[1] = 0; tri[2] = 3;
    23.  
    24.         tri[3] = 1; tri[4] = 0; tri[5] = 2;
    25.  
    26.         Mesh mesh = new Mesh(); mesh.name = "Plane"; mesh.vertices = verts; mesh.triangles = tri; mesh.uv = uv; mesh.normals = normals; mf.mesh = mesh;
    27.     }
    28. }
    29.  
     
  2. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    You need to add the MeshRenderer component, there is no component called just Renderer (though that is the base class for all other renderers.
     
    WallerTheDeveloper likes this.
  3. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    916
    The problem is that the object Cube that the script is attached to it have already Mesh Renderer component and still when i'm running the game i'm getting the exception:

    MissingComponentException: There is no 'Renderer' attached to the "Plane" game object, but a script is trying to access it.
    You probably need to add a Renderer to the game object "Plane". Or your script needs to check if the component is attached before using it


    And i can't add another Mesh Renderer to the Cube.


    A screenshot:

     
  4. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    Well, you need to add a MeshRenderer to the plane as that is the object you try to get the renderer from:
    Code (CSharp):
    1. GameObject tmpobj = new GameObject(); MeshFilter mf = tmpobj.AddComponent<MeshFilter>();
    2.         tmpobj.name = "Plane"; tmpobj.GetComponent<Renderer>().material.color = new Color(0.9490f, 0.8901f, 0.7686f, 0);
    3.  
     
    raining_day0513 likes this.