Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

Changing Skybox material through script

Discussion in 'Scripting' started by Aurecon_Unity, Mar 1, 2012.

  1. Aurecon_Unity

    Aurecon_Unity

    Joined:
    Jul 6, 2011
    Posts:
    232
    Hi All

    I am trying to do something seemingly simple - change the material of a global skybox through script.

    However I can't seem to get it to work... i've looked up a few similar threads on the net but it seems that there is no 'material' property for the RenderSettings.skybox?

    This property is even mentioned in the docs, so any reason why it's not working? My code is fairly simple:

    Code (csharp):
    1. var MatTest : Material;
    2.  
    3. function Start () {
    4.  
    5. Skybox.material = MatTest;
    6.  
    7. }
    It also refuses to work if I use RenderSettings.skybox.material.

    Any ideas?
     
    Dalv_Olan likes this.
  2. UnityCoder

    UnityCoder

    Joined:
    Dec 8, 2011
    Posts:
    534
    Use this code, i tested it in my project and its work perfectly:



    var mat1:Material;

    var mat2:Material;



    function Start(){

    RenderSettings.skybox=mat2;

    }
     
    Last edited: Mar 1, 2012
  3. Aurecon_Unity

    Aurecon_Unity

    Joined:
    Jul 6, 2011
    Posts:
    232
    Thank you Ram.

    I see from your code that the mistake I made was thinking I need to have '.material' after skybox.

    I appreciate the help.
     
    UnityCoder likes this.
  4. Mohammadalk

    Mohammadalk

    Joined:
    Nov 28, 2013
    Posts:
    1
    Can you show me how to change the skybox materials during the run time by a button named "change skybox" step by step plz? I am totally new to scripting. Thanks!
     
  5. Alexandre Nunes

    Alexandre Nunes

    Joined:
    May 24, 2013
    Posts:
    1
    to Change a skybox during the run time its easy

    ========( SCRIPT )========

    var mat1:Material;
    var mat2:Material;

    function OnGUI(){
    if(GUI.Button(Rect(0,0,100,20),"Material 1")){
    RenderSettings.skybox=mat1;
    }
    if(GUI.Button(Rect(30,0,100,20),"Material 2")){
    RenderSettings.skybox=mat2;
    }
    }

    ========================
     
    juraj123, paulrich_nb and UnityCoder like this.
  6. monsterbuzz786

    monsterbuzz786

    Joined:
    Oct 17, 2016
    Posts:
    22
    it seems like trender settings has changed the standard method for setting materials from strings. is there another way to set skybox materials from code?

    Code (csharp):
    1.  
    2.         //change skybox mat
    3.  
    4.         Material levelMat = new Material(Application.dataPath + "/Materials/chec"+ levelCount +".mat");
    5.         RenderSettings.skybox = levelMat;
     
  7. gaiastellar

    gaiastellar

    Joined:
    Nov 8, 2013
    Posts:
    46
    Hi,
    I'm trying to change the individual textures on a skybox material at runtime.

    Ive created texture2d from render texture via script but I want to assign that to one of the 6 skybox material textures.

    Any help please?

    Thanks
     
  8. rizi444

    rizi444

    Joined:
    Jun 1, 2017
    Posts:
    1
    1. var otherSkybox : Material; // assign via inspector
    2. function OnTriggerEnter(other : Collider)
    3. {
    4. RenderSettings.skybox = otherSkybox;
    5. }
     
  9. Rs

    Rs

    Joined:
    Aug 14, 2012
    Posts:
    39
    If you are using dynamic GI you also need to call DynamicGI.UpdateEnvironment()

    Code (CSharp):
    1.  
    2. RenderSettings.skybox = yourSkyboxMaterial;
    3. DynamicGI.UpdateEnvironment();
     
  10. Benjoe

    Benjoe

    Joined:
    Dec 30, 2017
    Posts:
    2
    Hi! My problem is that I want to create a Time of Day script in JavaScript, here is the script, named MovingTheSun applied on the directional light:

    Code (csharp):
    1. #pragma strict
    2. public static var Rot;
    3.  
    4.  
    5. function Update () {
    6.     transform.Rotate(Vector3.right * Time.deltaTime / 5);
    7.     Rot = Input.GetAxis("Vertical");
    8. }
    This is all right, but here's the other script, named SkyboxChanging applied on the camera:
    Code (csharp):
    1. var night : Material;
    2. var evening : Material;
    3. var darkday : Material;
    4. var day : Material;
    5.  
    6.  
    7. function Update () {
    8.     if (MovingTheSun.Rot >= 65) {RenderSettings.skybox = day;}
    9.     else if (MovingTheSun.Rot < 65 && MovingTheSun.rot >= 0) {RenderSettings.skybox = darkday;}
    10.     else if (MovingTheSun.Rot < 0 && MovingTheSun.rot >= -65) {RenderSettings.skybox = evening;}
    11.     else {RenderSettings.skybox = night;}
    12.     DynamicGI.UpdateEnvironment();
    13. }
    This code keeps me giving this error: Assets/SkyboxChanging.js(9,65): BCE0044: unexpected char: 0xF013.
    I don't know what to do with it and I'm very angry.
    PLEASE HELP!
     
  11. Benjoe

    Benjoe

    Joined:
    Dec 30, 2017
    Posts:
    2
    OK, this problem is solved, but here's another problem: why is "Rot" in MovingTheSun an Object type variable??
     
  12. J5Dev

    J5Dev

    Joined:
    Nov 19, 2017
    Posts:
    1
    Hi,

    Not sure about from a JS perspective, but pretty sure the rotation is a Quaternion, what you should be looking at is the rotation of teh X axis (or whichever you have used to rotate the sun around.