GetComponent not working... Am I going crazy?

Hello

I made a very simple code, that simply fetch the mesh and the material on some gameObject, but they return null, despite the gameobjects being in the scene and having said component, what did I forget?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RenderTest : MonoBehaviour {
    RenderTexture RT;
    public Material mat;
    public GameObject g;
    Mesh c;
    Material m;


    private void Awake()
    {
        RT = new RenderTexture(512, 512, 24);
        c = g.GetComponent<Mesh>();
        m = this.gameObject.GetComponent<Material>();
    }
    void Start () {
        Debug.Log(c);


        SurfaceCanvas.init(RT, mat);
        //...
        SurfaceCanvas.close();

        m.SetTexture("albedo", RT);
       
    }

6215246--683237--cube unity.png 6215246--683240--material script.png

“Mesh” and “Material” aren’t components. MeshFilter and MeshRenderer are components, and those components contain .mesh and .material properties respectively.

4 Likes

I knew I was dumb, because it’s not the first time I do it, I still forgot, dang

Honestly, it’s bizarre that GetComponent() doesn’t result in a compiler error. There’s no reason at all for that generic method not to have a Component type restriction on the given type.

4 Likes