Search Unity

Changing Standard Shader Emission through script - Issues

Discussion in 'Scripting' started by CastleIsGreat, Apr 27, 2015.

  1. CastleIsGreat

    CastleIsGreat

    Joined:
    Nov 10, 2014
    Posts:
    176
    Hi, I'm currently making a level where I want lights that are controlled through script to be able to be turned on and off through events in the game, which make a function call.

    So basically the problem I am encountering is that when the game is started, and the event takes place that calls the function to change the emissive texture, and emissive float value, both the float value and the emissive texture are applied, but the actual emission in game from this map and new set value does not take effect. It is not until I manually move around the value inside of the Inspector does it realize that it's emissivity is set to 1, and then it sets it.

    E.G.

    Code (CSharp):
    1. Using UnityEngine;
    2. Using System.Collections;
    3.  
    4. Texture texture;
    5. Material mat;
    6.  
    7. void SetEmissive(){
    8. mat.SetFloat("_EmissionScaleUI", 1);
    9. mat.SetTexture("_EmissionMap", texture);
    10. }
    Now when that SetEmissive is called, both are set correctly on material mat, but INGAME no emission is actually done. HOWEVER when I go to the inspector and barely move the slider down to .99 it then realizes it was set at 1, and starts emitting.

    It may just be me not understanding how this is supposed to work but if anyone could help me figure this out that would be great.
     
  2. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    Take a look at this forum thread here. It has a solution and a link to a Unity Answers explanation to why the solution works.
     
  3. CastleIsGreat

    CastleIsGreat

    Joined:
    Nov 10, 2014
    Posts:
    176
    Interesting, I've been playing around with values, and now it seems I'm getting material is animated, but not configured to support emissive, so what happens is I get a little notice saying that on the inspector, is there a way to reconfigure the material from script?
     
  4. CastleIsGreat

    CastleIsGreat

    Joined:
    Nov 10, 2014
    Posts:
    176
    I think I just had an AHAAAA moment, I'll set the emissive to almost nothing to make sure it is indeed configured to support it.
     
  5. CastleIsGreat

    CastleIsGreat

    Joined:
    Nov 10, 2014
    Posts:
    176
    That may have been the problem all along. Thanks a bunch though, your color thing is what made the error popup in the first place, when you just change the float it doesnt give you a warning, however setting a color (though it has a white one by default) set off the warning.
     
    Ben-BearFish likes this.
  6. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    No problem.
     
  7. CastleIsGreat

    CastleIsGreat

    Joined:
    Nov 10, 2014
    Posts:
    176
    Yeah I'm not sure if it should be considered a bug though, seems to me initially atleast, that changing either of the two (color or integer value) should set off the warning message, may have to send that in as a suggestion to unity, as it made more sense once I saw the warning what may have been causing the issue. Everything is up and working as I intended though.
     
    Ben-BearFish likes this.