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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Code error help CS0120

Discussion in 'Scripting' started by Michealfame, Aug 31, 2018.

  1. Michealfame

    Michealfame

    Joined:
    Dec 11, 2017
    Posts:
    11
    Error Message: Error CS0120 An object reference is required for the non-static field, method, or property 'Camera.depthTextureMode'



    private static void NewMethod5()
    {
    Camera.depthTextureMode |= DepthTextureMode.DepthNormals;
    }

    private void CreateMaterials ()
    {
    if (!m_SSAOMaterial && m_SSAOShader.isSupported)
    {
    m_SSAOMaterial = CreateMaterial (m_SSAOShader);
    m_SSAOMaterial.SetTexture ("_RandomTexture", m_RandomTexture);
    }
    }

    [ImageEffectOpaque]
    void OnRenderImage (RenderTexture source, RenderTexture destination)
    {
    if (!m_Supported || !m_SSAOShader.isSupported) {
    enabled = false;
    return;
    }
    CreateMaterials ();

    m_Downsampling = Mathf.Clamp (m_Downsampling, 1, 6);
    m_Radius = Mathf.Clamp (m_Radius, 0.05f, 1.0f);
    m_MinZ = Mathf.Clamp (m_MinZ, 0.00001f, 0.5f);
    m_OcclusionIntensity = Mathf.Clamp (m_OcclusionIntensity, 0.5f, 4.0f);
    m_OcclusionAttenuation = Mathf.Clamp (m_OcclusionAttenuation, 0.2f, 2.0f);
    m_Blur = Mathf.Clamp (m_Blur, 0, 4);

    // Render SSAO term into a smaller texture
    RenderTexture rtAO = RenderTexture.GetTemporary (source.width / m_Downsampling, source.height / m_Downsampling, 0);
    float fovY = Camera.fieldOfView;
    float y = Mathf.Tan (fovY * Mathf.Deg2Rad * 0.5f) * Camera.farClipPlane;
    float x = y * Camera.aspect;
    m_SSAOMaterial.SetVector (Name, new Vector3(x, y, (float)Camera.farClipPlane));
    int noiseWidth, noiseHeight;
    if (m_RandomTexture) {
    noiseWidth = m_RandomTexture.width;
     
  2. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    1) Code tags, please.

    2) Do you have a camera instance? It looks like you're trying to set the depthTextureMode on the Camera class.
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Use CODE tags when posting code on the forum if you want people to actually try to read it. You're trying to assign a value to the entire Camera class rather than to a specific camera which you should be doing.

    You can use Camera.main to find the instance of a Camera in your scene that is tagged with MainCamera.

    https://docs.unity3d.com/ScriptReference/Camera-main.html
     
  4. Michealfame

    Michealfame

    Joined:
    Dec 11, 2017
    Posts:
    11
    I dont understand what you mean if i have a camera instance, i dont know much about c# coding
     
  5. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    I encourage you to take the time to learn the C# basics. It doesn't really take all that long, promise!

    https://unity3d.com/learn/tutorials/s/scripting

    In a nutshell:

    Suppose you have 5 cameras in your scene. How do you tell Unity that you want to modify the main camera's depth texture? You need some way to refer to THAT camera, and not any of the others.

    So the easiest way to do that is to use Camera.main, as Joe-Censored suggested. Your code would look like this:

    Code (csharp):
    1.  
    2. private static void NewMethod5()
    3. {
    4. Camera.main.depthTextureMode |= DepthTextureMode.DepthNormals;
    5. }
    6.  
    (note the use of "main" in Camera.main.depthTextureMode)
     
  6. Michealfame

    Michealfame

    Joined:
    Dec 11, 2017
    Posts:
    11
    you are awesome!!! it worked, wow. also thes 4 cameras at the bottom too those are showing the error as well they are highlight in the code i show at the bottom

    // Render SSAO term into a smaller texture
    RenderTexture rtAO = RenderTexture.GetTemporary (source.width / m_Downsampling, source.height / m_Downsampling, 0);
    float fovY = Camera.fieldOfView;
    float y = Mathf.Tan (fovY * Mathf.Deg2Rad * 0.5f) * Camera.farClipPlane;
    float x = y * Camera.aspect;
    m_SSAOMaterial.SetVector (Name, new Vector3(x, y, (float)Camera.farClipPlane));
    int noiseWidth, noiseHeight;
    if (m_RandomTexture) {
    noiseWidth = m_RandomTexture.width;
     
  7. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    Same thing. Add main to all of them.

    Of course it'd be better to grab the instance one time, then use that instance throughout your code, like so:

    Code (csharp):
    1.  
    2. Camera mainCamera = Camera.main;
    3. float fovY = mainCamera.fieldOfView;
    4. float x = y * mainCamera.aspect;
    5. etc...
    6.  
     
  8. Michealfame

    Michealfame

    Joined:
    Dec 11, 2017
    Posts:
    11
    wow my friend is there a way i can give back let me know
     
  9. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    No worries. Happy to help!
     
  10. Michealfame

    Michealfame

    Joined:
    Dec 11, 2017
    Posts:
    11
    I am getting a new Warning here!

    Warning: Warning CS0618 'SystemInfo.supportsRenderTextures' is obsolete: 'supportsRenderTextures always returns true, no need to call it'

    // Create the accumulation texture
    if (accumTexture == null || accumTexture.width != source.width || accumTexture.height != source.height)
    {
    DestroyImmediate(accumTexture);
    accumTexture = new RenderTexture(source.width, source.height, 0);
    accumTexture.hideFlags = HideFlags.HideAndDontSave;
    Graphics.Blit( source, accumTexture );
     
  11. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    That warning mentions
    supportsRenderTextures
    but that is not shown in your code. Basically, the compiler is telling you that you are running some code that adds no value (in other words, it simply wastes CPU cycles).

    Also, please :
    Code (CSharp):
    1. bool ShouldIUseCodeTagsWhenPastingCode()
    2. {   // Hell, yeah!  :)
    3.     return true;
    4. }
     
    Joe-Censored likes this.
  12. Michealfame

    Michealfame

    Joined:
    Dec 11, 2017
    Posts:
    11
    i am new to this forum please understand i am not used to forums.
    so please tell me what i should do in order to remove that which is not needed?
     
  13. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    If the script in which you are getting the error is not too large, can you paste the whole script and the whole error message. The error message will include a line number and then we can link the error message to your code.

    When you paste the code, be sure to use code tags (it is the button just above the text entry box you type in - if you hover over it, the tooltip says ""Insert code). :)
     
  14. Michealfame

    Michealfame

    Joined:
    Dec 11, 2017
    Posts:
    11
    hey bro your online??