- Home /
Question by
Recino · Dec 26, 2013 at 12:47 AM ·
c#errorgetcomponentrenderersystem.type
An object reference is required to access non-static member `UnityEngine.Component.GetComponent(System.Type)
Okay, I'm trying to make a door visible by turning on the mesh renderer, so I made this code, but I'm getting this error: Assets/scripts/NewDoor.cs(18,35): error CS0120: An object reference is required to access non-static member `UnityEngine.Component.GetComponent(System.Type)'
The script (in c#)
using UnityEngine;
using System.Collections;
public class Newdoor : MonoBehaviour {
GameObject [] dr;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
dr = GameObject.FindGameObjectsWithTag("door");
foreach(GameObject go in dr){
go.Component.GetComponent(renderer);
renderer.enabled = true;
}
}
}
I get the error is on line 18, and I'm sure that it's a very easy answer, but I just don't get it. Please help, I'm trying to do this for a class. Thanks in advance for your help!
Comment
Best Answer
Answer by robertbu · Dec 26, 2013 at 04:19 AM
Here's a fixup of your code:
using UnityEngine;
using System.Collections;
public class Newdoor : MonoBehaviour {
GameObject [] dr;
void Update () {
dr = GameObject.FindGameObjectsWithTag("door");
foreach(GameObject go in dr){
Renderer rend = go.GetComponent<Renderer>();
if (rend != null)
rend.enabled = true;
}
}
}
If you are sure that every object tagged 'door' will have a renderer, you can get red of the null check and just do:
foreach(GameObject go in dr){
go.GetComponent<Renderer>().enabled = true;
}