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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Changing Material Color in Build Does not work

Discussion in 'Scripting' started by sherlockturtle, Apr 14, 2015.

  1. sherlockturtle

    sherlockturtle

    Joined:
    Jan 23, 2012
    Posts:
    592
    Hello all! So what my code does in the editor and is supposed to do is look for a gameobect called Small and then within that RoofPart and set the color to the players color. This works fine in the editor, but if I build it they come out as completely black and I can actually see the objects behind them. Any ideas?

    In Editor:

    In Build:


    Changing color code:
    Code (csharp):
    1.  
    2. foreach(Transform childB in buildingInstance.transform){
    3.                     if(childB.name == "Small"){
    4.                
    5.                     foreach(Transform childC in childB)
    6.                     {
    7.                         if(childC.name == "RoofPart")
    8.                         {
    9.                         childC.GetComponent<Renderer>().material.color = StringToColor("red");
    10.                        //I put "Red" here because it is gotten from a different variable, but in testing I know that is not the problem. It does get the string.
    11.  }
    12.                     }
    13.  
    Color function because I have to save what the color is and can't save Unitie's colors:
    Code (csharp):
    1.  
    2. Color StringToColor(string ColorString){
    3.         Color ReturnColor = Color.white;
    4.         ColorString = ColorString.ToLower();
    5.         switch (ColorString)
    6.         {
    7.             case "red":
    8.                 ReturnColor = Color.red;
    9.                 break;
    10.             case "blue":
    11.                 ReturnColor = Color.blue;
    12.                 break;
    13.             case "yellow":
    14.                 ReturnColor = Color.yellow;
    15.                 break;
    16.             case "green":
    17.                 ReturnColor = Color.green;
    18.                 break;
    19.             case "white":
    20.                 ReturnColor = Color.white;
    21.                 break;
    22.             case "black":
    23.                 ReturnColor = Color.black;
    24.                 break;
    25.             case "clear":
    26.                 ReturnColor = Color.clear;
    27.                 break;
    28.             case "cyan":
    29.                 ReturnColor = Color.cyan;
    30.                 break;
    31.             case "gray":
    32.                 ReturnColor = Color.gray;
    33.                 break;
    34.             case "magenta":
    35.                 ReturnColor = Color.magenta;
    36.                 break;
    37.  
    38.             default:
    39.             ReturnColor = Color.gray;
    40.             break;
    41.         }
    42.  
    43.         return ReturnColor;
    44.  
    45.     }
    46.  
    47.  
     
    Last edited: Apr 14, 2015
  2. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    It might have something to do with the quality settings when you creating the build, such as the quality settings not set high enough. Try setting it to fantastic.

    If that does not do it, you might have to change the code to do the same thing but in a different way. Unity can be flaky sometimes.
     
  3. sherlockturtle

    sherlockturtle

    Joined:
    Jan 23, 2012
    Posts:
    592
    Thank you for the quick reply roger0! Putting the graphics at fantastic sadly did nothing to help. Any ideas oh what I should do?
     
  4. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    Unfortunately, I dont have any idea on how to fix the problem, but I can think of some workarounds.

    1. The fact that the material is changing black and transparent probably has something to do with the polygons being flipped the wrong way or being displayed incorrectly. Try making the polygons double sided. You can do that by making a copy of the polygons at the same location and flipping them the other direction. You will need to do that in a 3D program.

    2. If the roof is part of the same mesh as the house, Try separating the roof part as a separate gameobject.
     
    Last edited: Apr 14, 2015
  5. sherlockturtle

    sherlockturtle

    Joined:
    Jan 23, 2012
    Posts:
    592
    I tried with a default untiy object (a sphere) and the same thing happened. It is see through black. I am going to try some other ways to change the material color.
     
  6. sherlockturtle

    sherlockturtle

    Joined:
    Jan 23, 2012
    Posts:
    592
    Using material.color = and material.setcolor still result in the black that is see through even with different objects.

    I also thought maybe it was that the object was static, but changing that did not help.
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    On some platforms (in my particular case I saw this on an Android Nexus4 phone), if the texture has ANY alpha channel (even if it is completely opaque), the texture imports incorrectly. If you have a texture on this material, make sure it does not have an alpha channel, or else temporarily replace it with an opaque white one and see if the problem persists. It may give you insight into what is happening.
     
  8. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,516
    They're not "black", they're gone. That's why you can see the stuff behind them. The colour may in fact be correct.

    To start with, what kind of material/shader is on the object? More fundamentally, though, if you remove this colour code does the object still disappear? The issue might not be where you think it is.
     
  9. sherlockturtle

    sherlockturtle

    Joined:
    Jan 23, 2012
    Posts:
    592
    Thank you Kurt and Angry Penguin. It turned out to be that within projects settings > graphics unity was not loading the shaders with them. I added them in and everything worked. Thank you for the ideas though :)!