Unity3d MeshRender

I am trying to use C # to disable and enable the MeshRender component in Unity3d, however I get the following error:

error CS0120: An object reference is required to access the non-stationary member `UnityEngine.GameObject.GetComponent (System.Type) '

Below is the line of code that I am using. I use this in the same function.

 MeshRenderer showZone = GameObject.GetComponent<MeshRenderer>(); 

I also post here, not Unity Answers, since I get a much faster answer here, and this is always useful information regardless of the result.

+6
source share
14 answers

You have problems with several problems. First, you are trying to use GetComponent<> for a class instead of an instance of an object. This is directly related to your second problem. After searching for a specific GameObject you are not using the result, and you are trying to disable the GameObject renderer containing the script. Third, C # is case sensitive, Renderer is a class, and Renderer is a reference to a Renderer instance attached to a GameObject

This piece of code combines everything: find a GameObject and turn off its rendering

 GameObject go = GameObject.FindWithTag("zone1"); if (go != null) { // the result could be null if no matching GameObject is found go.renderer.enabled = false; } 

You can use go.GetComponent<MeshRenderer>().enabled = false; instead of go.renderer. enabled = false; go.renderer. enabled = false; But using Renderer , you donโ€™t need to know what kind of rendering GameObject uses. For example, it can be MeshRenderer or SpriteRenderer , Renderer always points to the rendering used by GameObject , if one exists.

+17
source

A friend of mine. Just try using lowercase gameObject instead of gameObject and renderer instead of renderer

The main problem you are trying to access a variable of a static class by using the class name instead of the class instance.
Class names here gameObject and renderer
And instances of gameObject and renderer

+2
source
 MeshRenderer showZone = GetComponent<MeshRenderer>(); 

remove "GameObject".

GameObject is a type. What you need is an instance of the gameObject to call GetcComponent. This is what it is all about.

What a note:

 MeshRenderer showZone = GetComponent<MeshRenderer>(); 

same as:

 MeshRenderer showZone = this.GetComponent<MeshRenderer>(); 

You call GetComponent on the GameObject instance to which the script is bound.

+2
source

your code should look like this:

 MeshRenderer showZone = GetComponent<MeshRenderer>(); 
+2
source

As others have already written, you need to get an instance of GameObject . You call the base class GameObject , where only static functions can be called that do not need GameObject in SceneView .

GameObject Is an instance of an instance. You get a GameObject instance added to Monobehaviour . Calling the GetComponent function without any object is the same as:

  • this
  • GameObject

GameObject No instance.

Be careful in the first letter!

+1
source

See the 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 programming languages โ€‹โ€‹in the upper right corner.

0
source

2 ways to solve the problem. You add the word static to the method invoking your statement.

ex: public static GetTheMesh () {}

I do not recommend doing this. If you have other calls inside the method that should access the instance, this will cause problems.

The second way to commit is to first make a pointer or link before getting the component. Or use GameObject.Find <=, which is slow if.

 showZone = GameObject.Find("TheGameObjectName").GetComponent<MeshRenderer>(); 
0
source

If you want to disable the renderer on this gameObject, use:

 this.GetComponent<Renderer>().enabled = false; 

If this is not this gameObject, then use:

 GameObject.FindGameObjectWithTag("your_tag").GetComponent<Renderer>().enabled = false; 

Or you can pass the object manually:

 public GameObject go; go.GetComponent<Renderer>().enabled = false; 

https://docs.unity3d.com/ScriptReference/Renderer-enabled.html

0
source

you can use two types of code world to access MeshRenderer to enable and disable

1> create a GetMeshRenderer script (the script name as you want) attached to the empty game object in the scene and assign a cube or sphere or any 3D object as you 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 script world code 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; } } 

}

0
source

Your problem is that you are using GameObject , which is just a class that โ€œdescribesโ€ what it is. What do you want if this script is attached to a GameObject that wants to render a mesh rendering, GameObject with a lowercase "g."

If you want to get a mesh rendering of another GameObject, you can find it by name using GameObject.Find("zone1"); (note the uppercase "G" in this), or you can give it a tag and find it using GameObject.FindGameObjectWithTag("zone1"); (You may or may not know this, but this does not prevent you from providing information.)

Edit:

Another problem is that you should use renderer instead of renderer , because, like a GameObject "Renderer" with a capital of "R" refers to a class instead of a specific object.

0
source

The problem of GameObject is different from GameObject Gameobject is a class, and gameobject is an instance of the current game object or game object into which the script is bound.

Replace string

 MeshRenderer showZone = GameObject.GetComponent<MeshRenderer>(); 

with

 MeshRenderer showZone = GameObject.GetComponent<MeshRenderer>(); 

I think it will be done,

Also note that your Error statement says that GameObject is a class or data type, not an object

0
source

Do you see gears? Yes? Click and click remove component.

0
source

You can use the declaration:

 other.gameobject.GetComponent< MeshRenderer>().Setactive (false); 

Link to one line ... after the condition is met.

For more information on MeshRender, see Unity docs.

0
source

I had the same problem with my declaration, and I just fixed it by changing "G" to "g" in gameObject, and I declared it in Start, so it looks like ...

MeshRenderer showZone = gameObject.GetComponent ();

0
source

Source: https://habr.com/ru/post/970872/


All Articles