Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AmasterAmaster · Jun 30, 2015 at 11:55 AM · unity 5texturematerialprogrammingrenderer

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:

alt text

The above picture is the deck of cards separated by some space. The below picture is a separate card.

alt text

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?

card.jpg (29.5 kB)
deckofcards.jpg (22.4 kB)
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bikupan · Jun 30, 2015 at 12:18 PM 0
Share

Could be that the textures are null, where do you define the textures?

avatar image AmasterAmaster · Jun 30, 2015 at 10:38 PM 0
Share

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).

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Changing two different objects renderer colour 1 Answer

Having wrong texture on my model? 1 Answer

Renderer Removed When Scene Started 0 Answers

Why are my textures on planes flipped / reversed left-to-right? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges