Unity doesnt seem to recognise my C# Script.

Hi, i wrote this code, and unity doesnt seem to be running it.For example, nothing is working in the code.
Unity version: 2019.2.f1
Visual Studio version: 2019
--------------------------Code-----------------------------

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.PostProcessing;
using UnityEngine.UI;

public class GraphicsManager : MonoBehaviour
{
    public PostProcessingProfile Graphics;
    public PostProcessingProfile low;
    public PostProcessingProfile medium;
    public PostProcessingProfile high;
    public PostProcessingProfile ultra;
    public PostProcessingBehaviour player;
    public Camera Player;

    public Slider AmbientOcclusion;
    public Text AmbientOcclusionDisp;
    public float AO;

    public Slider Antialiasing;
    public Text AntialiasingDisp;
    public float AntiAliasing;

    public Slider SSR;
    public Text SSRDisp;
    public float Ssr;

    public Slider DOF;
    public Text DOFDisp;
    public float DepthOfView;

    public Slider MotionBlur;
    public Text MotionBlurDisp;
    public float MBlur;

    public Slider Bloom;
    public Text BloomDisp;
    public float BloomValue;

    public Slider ColourGrading;
    public Text ColourGradingDisp;
    public float CGrading;

    public Slider ChromaticAberration;
    public Text ChromaticAberrationDisp;
    public float CAberration;

    public Slider Vignette;
    public Text VignetteDisp;
    public float VignetteValue;

    public Slider Fog;
    public Text FogDisp;
    public float FOG;

    public GameObject GraphicsMenu;
    public bool OnOff;

    public void Update()
    {
        AO = AmbientOcclusion.value;
        AmbientOcclusionDisp.text = "" + AO;
        Graphics.ambientOcclusion.settings.intensity.Equals(AO);

        Ssr = SSR.value;
        SSRDisp.text = "" + Ssr;
        Graphics.screenSpaceReflection.settings.intensity.Equals(Ssr);

        DepthOfView = DOF.value;
        DOFDisp.text = "" + DepthOfView;
        Graphics.depthOfField.settings.focusDistance.Equals(DepthOfView);

        MBlur = MotionBlur.value;
        MotionBlurDisp.text = "" + MBlur;
        Graphics.motionBlur.settings.frameBlending.Equals(MBlur);

        BloomValue = Bloom.value;
        BloomDisp.text = "" + BloomValue;
        Graphics.bloom.settings.intensity.Equals(BloomValue);

        CAberration = ChromaticAberration.value;
        ChromaticAberrationDisp.text = "" + CAberration;
        Graphics.chromaticAberration.settings.intensity.Equals(CAberration);

        VignetteValue = Vignette.value;
        VignetteDisp.text = "" + VignetteValue;
        Graphics.vignette.settings.intensity.Equals(VignetteValue);

        if (Input.GetKeyDown("Esc"))
        {
            OnOff = !OnOff;
            GraphicsMenu.SetActive(OnOff);
        }
    }

    public void Low()
    {
        Player.GetComponent<PostProcessingBehaviour>().profile = low;
        player.GetComponent<PostProcessingProfile>().Equals(low);
    }

    public void Medium()
    {
        Player.GetComponent<PostProcessingBehaviour>().Equals(medium);
    }

    public void High()
    {
        Player.GetComponent<PostProcessingBehaviour>().Equals(high);
    }

    public void Ultra()
    {
        Player.GetComponent<PostProcessingBehaviour>().Equals(ultra);
    }
}

Can you give us further details, what do you see? Did you attach this on a game object? Is it enabled? is this code in a file called GraphicsManager.cs? Do you have any errors in the console?

2 Likes

Well, here it is, as you see, it was suppose to change the PostProcessing profile in the player camera but it doesnt.
my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.PostProcessing;
using UnityEngine.UI;

public class GraphicsManager : MonoBehaviour
{
    public PostProcessingProfile Graphics;
    public PostProcessingProfile low;
    public PostProcessingProfile medium;
    public PostProcessingProfile high;
    public PostProcessingProfile ultra;
    public PostProcessingBehaviour player;
    public Camera Player;

    public void Low()
    {
        Player.GetComponent<PostProcessingBehaviour>().profile = low;
        player.GetComponent<PostProcessingProfile>().Equals(low);
    }

    public void Medium()
    {
        Player.GetComponent<PostProcessingBehaviour>().Equals(medium);
    }

    public void High()
    {
        Player.GetComponent<PostProcessingBehaviour>().Equals(high);
    }

    public void Ultra()
    {
        Player.GetComponent<PostProcessingBehaviour>().Equals(ultra);
    }
}

In the editor

You have errors in the console. You need to fix them.

2 Likes

Coming from other script… I’ll fix them

What for the script above?

Several things:

First, you can see at the bottom of your screenshot that you are getting at least one error. If you switch to the “Console” window, you’ll be able to see if there are more errors, and (usually) where in the code they are being thrown. Based on what I can see of the error, it’s probably not coming from the specific file you are editing, but it could still be blocking you.

Note that if you get compile-time errors, then Unity will stop compiling your code and you won’t be able to run at all. If you get run-time errors, then the specific function that threw the error will abort (which means none of the code behind it in that function will run), but Unity will try to continue running other parts of your game.

Second, you haven’t shown anything that calls the specific functions you defined in your code, like Low(). If you make up your own functions, you also need to tell the program when you want to execute them. If you haven’t done that, then Unity won’t even try to run your code, because you didn’t ask it to.

If you want to test whether your code is even running, use Debug.Log() to write something to the console at the top of the function. If that message shows up, then you know the function got called, and so if it didn’t do what you want, there’s something wrong inside the function. If the message doesn’t show up, then the problem is with something outside the function–it never got started in the first place.

Third, most of your code makes absolutely no sense, and would have no visible effects even if it did run. The “Equals” function doesn’t make two things equal, it tests whether two things are already equal. Calling it just gives you information, it doesn’t change anything that’s happening.

Even if it set values rather than checking them, “setting” a component to be another component doesn’t make any sense as a concept. You can set a variable inside of a component, or you can add a new component or remove an existing one, but you can’t “set” a component to be another component. Not sure what you’re hoping to accomplish, but I’m pretty sure you’re headed in absolutely the wrong direction.

The tricky thing you have to remember when computer programming is that computer are like evil genies: they’ll grant your wishes, but you better phrase your wishes exactly right, because they’re going to give you precisely what you asked for, NOT what you meant. You can’t just grab some function that sounds kinda like what you want and expect the computer to figure out what you’re aiming for. Computers only do exactly what you told them, no matter how little sense it makes.

It looks like you need to take a step backwards, describe what you are hoping to accomplish, and figure out a new way of approaching that.

1 Like

Thanks for your help, I’m just a 16yrs old boy and trying to make a graphics menu. Well, I’ve created different Profiles and would like to change it via script on a button pressed.Cam you help with the code please

If you are talking about Unity quality levels, you can set the current level from script by calling QualitySettings.SetQualityLevel.

I happen to have a script here that can be attached to a TextMeshPro DropDown in order to automatically populate it with the available quality levels and switch to whatever the user selects:

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

[RequireComponent(typeof(TMP_Dropdown))]
public class QualityDropdown : MonoBehaviour
{
    public bool applyExpensiveChanges = true;

    protected TMP_Dropdown dropdown;
    protected bool suppressCallbacks;

    protected virtual void Awake()
    {
        dropdown = GetComponent<TMP_Dropdown>();
        if (dropdown == null)
        {
            Debug.LogError("No dropdown found on object " + gameObject.name, gameObject);
        }
        else
        {
            dropdown.onValueChanged.AddListener(UponSelection);
        }
    }

    protected virtual void OnEnable()
    {
        Refresh();
    }

    // Updates list of qualities
    public virtual void Refresh()
    {
        try
        {
            suppressCallbacks = true;
            List<string> qualityNames = new List<string>(QualitySettings.names);
            dropdown.ClearOptions();
            dropdown.AddOptions(qualityNames);
            dropdown.value = QualitySettings.GetQualityLevel();
        }
        finally
        {
            suppressCallbacks = false;
        }
    }

    // Change quality level to match selection
    protected virtual void UponSelection(int selectedQuality)
    {
        if (suppressCallbacks) return;
        QualitySettings.SetQualityLevel(selectedQuality, applyExpensiveChanges);
    }
}

If quality levels aren’t what you mean by “profiles”, then you’ll have to describe more.

No, here low,medium,high and ultra refers to different PostProcessing Profiles that I have set up.

I don’t know what those are, and searching Unity’s documentation isn’t turning up anything.

There’s something in the asset store with that name, but it appears to be made by a third party. If that’s what you’re using, you’ll probably need to read whatever documentation they gave you on how to use it, or ask them for help. It’s not impossible to find someone on the forum who can help you with a specific third-party plugin, but the pool of people who know how to use it tends to be smaller than for standard Unity stuff.

However, you should also note that basic stuff like “make a button call a script that I wrote” is a standard thing that most people will expect you to already know how to do. If you need help with that level of stuff, you should probably take a look at some of Unity’s tutorials.

No linking a button to a script was the first thing I learned, but my question is how to actually change PostProcessing Profiles on a camera during runtime via script

If you’re using the post processing V2 stack the profiles are attached to post processing volumes, which are just objects in your scene. The way I do it is I have volumes for each quality level, and I have them all disabled except the one I want enabled. If the user changes the quality level mid game, I just disable the currently active pp volume and enable the one corresponding to their new quality level.

I’m using an older version of the pp v2 stack, but I’d be surprised if how it works has changed that much.

If you’re using the old pp v1, if I remember correctly you just set the reference on the script attached to the camera object to a new post processing profile. I had a manager script which just had references to all the post processing profiles I would ever use, and I’d just use that to swap the profiles on camera on the fly.

Ok thanks, I’ll give it a try

It does not matter from what script they come. In case of exception in any script unity stops frame processing, wich means this frame some updates has been called and others has not. After that scene state is invalid and anything can happen. So if you see something strange and errors in console you first fix errors despite where they coming from and then check again if strage thing persits.

1 Like

For the pp v1 stack, how you change the profile which code?

If I remember correctly, you just get a reference to the pp component attached to the camera, and just change which profile it references.

1 Like

Like
public PostProcessingProfile Pp;
Public Camera Player;
Player.GetComponent.(). PostProcessingProfile = Pp;