- Home /
Setting Textures causes GameObject Material to turn black?
I have no idea why setting materials causes my GameObjects to show up as black (does that mean it is null? But the inspector says otherwise). Could this be a bug in Unity or is there a problem with my logic? What I am trying to do is to set a texture on a created GameObject plane through scripting (and depending on what inputs I put in will show a different texture (example: a playing card face texture)). I also want to avoid using Instanciate (unless it can't be avoided...) because the "sharedMaterial" variable has caused me problems on making duplicate textures on cards that have different variable information. Here is what I have:
for(int i = currentDeck.deck.Length - 1; i > 0; i--)
{
//Create the card holder (holds the front and back of the card)
GameObject cardClone = new GameObject(i + ": " + currentDeck.deck[i].cardName);
//Create the card front as well as transform, rotation, scale, and parenting it
GameObject cardCloneFront = GameObject.CreatePrimitive(PrimitiveType.Plane);
cardCloneFront.transform.parent = cardClone.transform;
cardCloneFront.transform.rotation = Quaternion.Euler(90f, 0f, 0f);
cardCloneFront.transform.localScale = new Vector3(0.225f, 0f, 0.325f);
//Create the card back as well as transform, rotation, scale, and parenting it
GameObject cardCloneBack = GameObject.CreatePrimitive(PrimitiveType.Plane);
cardCloneBack.transform.parent = cardClone.transform;
cardCloneBack.transform.rotation = Quaternion.Euler(270f, 0f, 0f);
cardCloneBack.transform.localScale = new Vector3(0.225f, 0f, 0.325f);
//Place the card holder where it needs to be located (with rotation)
cardClone.transform.position = new Vector3(currentDeck.transform.position.x, currentDeck.transform.position.y + (i * 0.02f), currentDeck.transform.position.z);
cardClone.transform.rotation = Quaternion.Euler(90f, 0f, 0f);
//Add components and set card properties
cardClone.AddComponent<Card>();
cardClone.GetComponent<Card>().SetCard(currentDeck.deck[i]);
cardClone.GetComponent<Card>().front = cardCloneFront;
Material myNewMaterial = new Material(Shader.Find("Diffuse"));
cardCloneFront.GetComponent<Renderer>().material = myNewMaterial;
//cardClone.GetComponent<Card>().SetCardFront(currentDeck.deck[i].frontTexture);
cardCloneFront.GetComponent<Renderer>().material.SetTexture("_MainTex", currentDeck.deck[i].frontTexture);
cardClone.GetComponent<Card>().back = cardCloneBack;
cardClone.GetComponent<Card>().SetCardBack(currentDeck.deck[i].backTexture);
//card.GetComponent<Card>().SetCard(currentDeck.deck[i]);
//GameObject cardClone = (GameObject)Instantiate(card, new Vector3(currentDeck.transform.position.x, currentDeck.transform.position.y + (i * 0.02f), currentDeck.transform.position.z), Quaternion.Euler(90, 0, 0));
//GameObject test = (GameObject)Instantiate(cardClone, new Vector3(currentDeck.transform.position.x, currentDeck.transform.position.y + (i * 0.02f), currentDeck.transform.position.z), Quaternion.Euler(90, 0, 0));
}
If my code looks like that it is all over the place, it is because I was trying 3 different attempts at trying to get this to work. Here is what it looks like currently:
The above picture is the deck of cards separated by some space. The below picture is a separate card.
This has effected both sides of the cards as seen above. Those are the cards with (what looks like) missing textures, but the inspector says that these textures exist. So what is causing this?
Could be that the textures are null, where do you define the textures?
It should not be null, it is assigning the texture according to the debugger, and the Unity inspector says that there is a unique card face material on the card (as the picture can be seen in the inspector), but only shows black while playing the scene. I have defined them dynamically depending on what input I give them (by passing a number and finds the card through a file system, the card database handles that which I will show here)
public static Card GetCardInfo(int serialNumber, Card card)
{
//Temp card
Card c = card;
//Temp variables
string fileString = "";
string fileString2 = "";
//Find the card face
Texture2D tex = new Texture2D(2, 2);
//Loop through all the string files
for(int i = 0; i < imageFiles.Length; i++)
{
//If the image files are NOT null...
if(imageFiles[i] != null)
{
//String manipulation (early)
fileString = imageFiles[i].Substring(filesLocation.Length + 1);
fileString2 = fileString.Remove(fileString.IndexOf(".jpg"));
}
//If I miss the "imageFiles" for whatever reason (it became null), become robust and try filling out the array dynamically...
if(imageFiles[i] == null)
{
//Fill up the array
imageFiles = Directory.GetFiles(filesLocation, "*.jpg", SearchOption.TopDirectoryOnly);
//String manipulation (late)
fileString = imageFiles[i].Substring(filesLocation.Length + 1);
fileString2 = fileString.Remove(fileString.IndexOf(".jpg"));
//THEN, if the serial number is equal to the image file name (without extention)...
if(serialNumber == int.Parse(fileString2))
{
//Pick the one that matches the card art
tex.LoadImage(File.ReadAllBytes(imageFiles[i]));
break;
}
}
//Else, if the serial number is equal to the image file name (without extention)...
else if(serialNumber == int.Parse(fileString2))
{
//Pick the one that matches the card art
tex.LoadImage(File.ReadAllBytes(imageFiles[i]));
break;
}
}
c.SetCardFrontTexture((Texture)tex);
c.SetCardBackTexture((Texture)Resources.Load("cover", typeof(Texture)));
}
And is then called by the Card class that sets the texture directly:
public void SetCardFrontTexture(Texture cardFront)
{
this.frontTexture = cardFront;
//Do something extra if the front of the card is not null...
if(this.front != null)
{
this.front.GetComponent<Renderer>().material.SetTexture("_$$anonymous$$ainTex", cardFront);
}
}
Sorry for all this complexity, but I do know that the above code does work and is tested and reliable (in this comment only), but I do not understand why it is not working in this instance with the above problem (where it is in another scene).
Answer by AmasterAmaster · Jul 01, 2015 at 05:02 AM
Ok, after tinkering with the code a little bit, I found out that I can just have a clone with Instantiating it AND making a separate material for it (thus stopping the duplicated materials on all the cards).
Material myNewMaterialFront = new Material(Shader.Find("Diffuse"));
Material myNewMaterialBack = new Material(Shader.Find("Diffuse"));
GameObject cardClone = (GameObject)Instantiate(card, new Vector3(currentDeck.transform.position.x, currentDeck.transform.position.y + (i * 0.02f), currentDeck.transform.position.z), Quaternion.Euler(90, 0, 0));
cardClone.GetComponent<Card>().SetCard(currentDeck.deck[i]);
GameObject cardCloneFront = cardClone.transform.GetChild(0).gameObject;
cardCloneFront.GetComponent<Renderer>().material = myNewMaterialFront;
cardCloneFront.GetComponent<Renderer>().material.SetTexture("_MainTex", currentDeck.deck[i].frontTexture);
GameObject cardCloneBack = cardClone.transform.GetChild(1).gameObject;
cardCloneBack.GetComponent<Renderer>().material = myNewMaterialBack;
cardCloneBack.GetComponent<Renderer>().material.SetTexture("_MainTex", currentDeck.deck[i].backTexture);