Reputation: 119
I'm trying to use C# to disable and enable the MeshRender
component in Unity3d however I am getting the following error,
error CS0120: An object reference is required to access non-static member `UnityEngine.GameObject.GetComponent(System.Type)'
The line of code I am using is below. I'm using this in the same function.
MeshRenderer showZone = GameObject.GetComponent<MeshRenderer>();
Also I'm posting here rather than Unity Answers as I get a far faster response here and it's always useful information regardless of the outcome.
Upvotes: 7
Views: 4317
Reputation: 249
I had same issue with my declaration and i just fixed it by changing "G" to "g" in gameObject and i declared it in Start so it is like...
MeshRenderer showZone = gameObject.GetComponent();
Upvotes: 0
Reputation: 1
You can use the declaration:
other.gameobject.GetComponent< MeshRenderer>().Setactive (false);
One Line Reference...after the condition is fulfilled..
For more precise help regarding MeshRender, see the Unity Documentations..
Upvotes: 0
Reputation: 1
the problem is GameObject
is different from gameobject
Gameobject is a class and gameobject is a instance of current gameobject or gameobject in which the script is attached
Replace the line
MeshRenderer showZone = GameObject.GetComponent<MeshRenderer>();
with
MeshRenderer showZone = gameobject.GetComponent<MeshRenderer>();
I think this will do,
Also note that in your Error statement ,it is saying that GameObject is a Class or a data type not an object
Upvotes: 0
Reputation: 5
Your problem is that you are using GameObject
which is just a class that "describes" what it is. What you want, if this script is attached to the GameObject who's mesh renderer you want, is gameObject
with a lowercase "g."
If you want to get the mesh renderer of another GameObject, you can find it by name with GameObject.Find("zone1");
(note the uppercase "G" in that one,) or you can give it a tag and find it with GameObject.FindGameObjectWithTag("zone1");
(You may or may not already know that but it doesn't hurt to provide the information.)
Edit:
Your other problem is that you must use renderer
instead of Renderer
because, like the GameObject
"Renderer" with a capital "R" references a class, instead of a specific object.
Upvotes: 0
Reputation: 11
you can use two types of peace of code for access MeshRenderer enable and disable
1> create GetMeshRenderer script (script name as you want) attached to empty game-object into the scene and assign Cube or sphere or any 3d object as u want to enable and disable.
************************************** Code ***************************
using UnityEngine;
using System.Collections;
public class GetMeshRenderer : MonoBehaviour
{
public MeshRenderer ShowZone;
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if(Input.GetKey(KeyCode.Y))
{
ShowZone.enabled = true;
}
if(Input.GetKey(KeyCode.N))
{
ShowZone.enabled = false;
}
}
}
2>
attach below peace of code script to any 3d object like sphere ,cube
*************************** code ***************************
using UnityEngine;
using System.Collections;
public class GetMeshRenderer : MonoBehaviour
{
private MeshRenderer ShowZone;
// Use this for initialization
void Start ()
{
ShowZone = gameObject.GetComponent<MeshRenderer> ();
}
// Update is called once per frame
void Update ()
{
if(Input.GetKey(KeyCode.Y))
{
ShowZone.enabled = true;
}
if(Input.GetKey(KeyCode.N))
{
ShowZone.enabled = false;
}
}
}
Upvotes: 0
Reputation: 1
If you want to disable the renderer on this gameObject then use:
this.GetComponent<Renderer>().enabled = false;
If it's not this gameObject then use:
GameObject.FindGameObjectWithTag("your_tag").GetComponent<Renderer>().enabled = false;
Or you could give the object manually:
public GameObject go;
go.GetComponent<Renderer>().enabled = false;
https://docs.unity3d.com/ScriptReference/Renderer-enabled.html
Upvotes: 0
Reputation: 6052
Like others already wrote, you need to get an instantiated GameObject
. You call the base class GameObject
where only static functions can be called which do not need a GameObject
in the SceneView
.
gameObject
IS AN instance.You get the instance of the GameObject
the Monobehaviour
is added to. Calling the function GetComponent
without any object is the same as:
this
gameObject
GameObject
IS NO instance.
Be careful at the first letter!
Upvotes: 1
Reputation: 175
your code should look like this:
MeshRenderer showZone = GetComponent<MeshRenderer>();
Upvotes: 2
Reputation: 1825
2 ways you can solve the Problem either. You add the word static to the method that is calling your statement.
ex : public static GetTheMesh(){}
I do not recommend on doing this cause. If you have other calls inside the method that needs to access Instance, this will cause you trouble.
Second way of fixing it is make a pointer or reference first before getting the component. Or use the GameObject.Find <= which is slow if.
showZone = GameObject.Find("TheGameObjectName").GetComponent<MeshRenderer>();
Upvotes: 0
Reputation: 1989
MeshRenderer showZone = GetComponent<MeshRenderer>();
delete the 'GameObject.'
GameObject is a type. What you want is in an instance of a gameObject to call GetcComponent on. Thats what the error is about.
Which for note, this:
MeshRenderer showZone = GetComponent<MeshRenderer>();
is the exact same as this:
MeshRenderer showZone = this.GetComponent<MeshRenderer>();
You are calling GetComponent on the GameObject instance of which the script is attached to.
Upvotes: 2
Reputation: 582
My friend. Just try use lowercase gameObject
instead of GameObject
and renderer
instead of Renderer
The main problem that you try access Static class variable, using the name of class instead of class instance.
Class names here are GameObject
and Renderer
And instances are gameObject
and renderer
Upvotes: 2
Reputation: 3234
You're having trouble with several problems. First, you are trying to use GetComponent<>
on a class instead of an instance of an object. This leads directly to your second problem. After searching for a specific GameObject
you're not using the result and you're trying to disable the renderer of the GameObject
containing the script. Third, C# is case-sensitive, Renderer
is a class while renderer
is a reference to an instance of Renderer
attached to the GameObject
This code snippet combines everything: find the GameObject
and disable its renderer
GameObject go = GameObject.FindWithTag("zone1");
if (go != null) { // the result could be null if no matching GameObject is found
go.renderer.enabled = false;
}
You could use go.GetComponent<MeshRenderer>().enabled = false;
instead of go.renderer. enabled = false;
But by using renderer
you don't need to know what kind of renderer is used by the GameObject
. It could be a MeshRenderer
or a SpriteRenderer
for example, renderer
always points to the renderer used by the GameObject
, if there exists one.
Upvotes: 18
Reputation: 652
Look documentation:
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
void Example() {
renderer.enabled = false;
}
}
Link: http://docs.unity3d.com/ScriptReference/Renderer-enabled.html Changing of programming languages upper-right.
Upvotes: 0