Search code examples
c#unity-game-engineunityscript

How to disable sunshaft effect on my main camera?


I have a main camera in my scene with a Sun Shafts (Script) ImageEffect on it. (I'm using Medieval Environment asset.)

I'd like to disable this effect programmatically when main camera reaches a specific coordinate.

Here is my C# script's code:

using UnityEngine;
using System.Collections;
using System;

    public class EnableComponents : MonoBehaviour
    {
        private SunShafts mySunShafts; //<<< Error CS0246 displays here (see below)


        void Start ()
        {
            mySunShafts = GetComponent<SunShafts>();
        }


        void Update ()
        {

            foreach(Camera c in GameObject.FindObjectsOfType(typeof(Camera))) 
            {
                if ((c.name == "Main Camera"))
                {
                    if ((c.transform.position.x < -10))
                    {
                        mySunShafts.enabled = false;
                    }
                }
            }
    }

Here is how SunShafts' code starts:

    @script ExecuteInEditMode
    @script RequireComponent (Camera)
    @script AddComponentMenu ("Image Effects/Sun Shafts")

    enum SunShaftsResolution {
        Low = 0,
        Normal = 1,
        High = 2,
    }

    enum ShaftsScreenBlendMode {
        Screen = 0,
        Add = 1,    
    }   

    class SunShafts extends PostEffectsBase 
    {       
        public var resolution : SunShaftsResolution = SunShaftsResolution.Normal;
        public var screenBlendMode : ShaftsScreenBlendMode = ShaftsScreenBlendMode.Screen;
...
...
...

When I try to debug the code, following errormessage displays:

"Error CS0246: The type or namespace name 'SunShafts' could not be found (are you missing a using directive or an assembly reference?) (CS0246) (Assembly-CSharp)"

Here you can see that SunShaft effect and the above written C# script belongs to the same camera. However (and this is my problem) the two scripts just "cannot see each other".

enter image description here

So what am I doing wrong? Why my C# script "cannot see" the Sun Shafts script written in JavaScript? How should I change my C# code to be able to disable the SunShafts effect on my camera in runtime?

======================== EDIT #1 ======================

C# script is already in Plugins folder:

enter image description here

and here is the errormessage:

enter image description here

===================== EDIT #2 ==============================

Here is the imported Standard Asset:

enter image description here

Now there's no problem with UnityStandardAssets.ImageEffects;

enter image description here

However I'm getting this error message:

enter image description here


Solution

  • Your code is missing the assembly import, therefore the compiler doesn't find the SunShaft class.

    Place

    using UnityStandardAssets.ImageEffects;
    

    below

    using System;
    

    to import the assembly.