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. Dismiss Notice

How to toggle an image effect script on a camera?

Discussion in 'Scripting' started by Rajmahal, Jul 24, 2014.

  1. Rajmahal

    Rajmahal

    Joined:
    Apr 20, 2011
    Posts:
    2,101
    Hi,

    I'm having difficulty toggline on or off an image effect on my main camera. I'm not sure exactly how to reference it. There isn't a class as far as I can tell for it. The image effect is in a script called "MobileBloomRaj" but I can't create a public reference to this nor can I get it from GetComponent() command. How exactly can I reference to it and turn it on or off depending on the value of a player pref?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    Image effects are normal components, and can be accessed as such. I suspect what you're running into here is one of Unity's inter-language shortcomings. Are the two scripts (the one from which you're trying to access the effect, and the effect itself) written in different languages? Try putting the image effect script inside a folder named 'Plugins' if so; that should force that one to compile first, and then you'll be able to access it from other scripts.

    Alternately, you can trying using the string version of GetComponent, if all you need to do is enable it and disable it:
    Code (csharp):
    1.  
    2. Component ie = GetComponent("MobileBloomRaj");
    3. ie.enabled = false;
    4.  
     
  3. Rajmahal

    Rajmahal

    Joined:
    Apr 20, 2011
    Posts:
    2,101
    Yes, I'm coding in C# and the image effect is coded in JavaScript. I'll try putting the javascript script into the plugins. I noticed I was able to get Unity's Bloom script working fine and I couldn't figure out what was different about this custom bloom script. Many thanks.
     
  4. Rajmahal

    Rajmahal

    Joined:
    Apr 20, 2011
    Posts:
    2,101
    Sorry, just to clarify ... regarding your sample code. I don't think enabled and disabled are allowed on the Component class. Is there some other way to toggle them on or off via code?
     
  5. Rajmahal

    Rajmahal

    Joined:
    Apr 20, 2011
    Posts:
    2,101
    Never mind ... moving the script to a plugin folder worked. Thanks again!
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    Oh, you're right. I always forget that .enabled isn't part of Component (I've never understood why not...). Using MonoBehaviour should work.