- Home /
Why are Textures applied to multiple materials script.
My question is about an issue on texturing materials and placing said materials onto objects through script. currently I have two buttons one to place a texture onto the front face of a card and one to place a texture onto the back face.
When I do it the first time it works fine only applying the texture onto the one face. However any subsequent presses and the texture for both materials that are on the faces change.
NOTE each face is a separate game object.
Updated the Code here with Edit: I did end up getting it to texture the front and back of cards and materials separately however I ran into another problem where when I created a copy of my card object and then changed the texture on it again it would then change the texture on the materials attached to the other cards as well. I currently have it set up so that each card has two cube objects back ot back one called "front" and one called "Back" could it be because of the commoon name that it is doing this meaning I have to change the names of said parts of objects?
// Scripts
public Colomn1 colomn1Script;
public Colomn2 colomn2Script;
public Colomn3 colomn3Script;
public Card aCard;
//Objects
public GameObject card;
public GameObject holdingCard;
public GameObject colomn1Object;
public GameObject colomn2Object;
public GameObject colomn3Object;
public GameObject frontObject;
public GameObject backObject;
public Texture2D frontTexture;
public Texture2D backTexture;
frontTexture = new Texture2D (1, 1);
backTexture = new Texture2D (1, 1);
colomn1Object = GameObject.Find ("Colomn1");
colomn2Object = GameObject.Find ("Colomn2");
colomn3Object = GameObject.Find ("Colomn3");
card = GameObject.Find ("Card");
holdingCard = null;
frontObject = GameObject.Find ("Front");
backObject = GameObject.Find ("Back");
void CreateMaterial (Texture2D texture, GameObject gameobject)
{
// Create a simple material asset
var amaterial = new Material (Shader.Find ("Diffuse"));
AssetDatabase.CreateAsset (amaterial, "Assets/Materials/" + cardName + ".mat");
amaterial.mainTexture = texture;
gameobject.renderer.material = amaterial;
//gameobject.renderer.material.mainTexture = texture;
// Print the path of the created asset
Debug.Log (AssetDatabase.GetAssetPath (amaterial));
}
if (GUILayout.Button ("Add Texture to front of Card"))
{
side = "front";
if (cardTypeToolbarInt == 0)
{
cardName = "Play_Play_" + seriesText + "_" + copy.ToString () + "_" + index.ToString () + "_" + side;
}
// camera.SelectCamera (0);
string file = EditorUtility.OpenFilePanel ("Hello", "", "png");
if (file.Length != 0)
{
WWW www = new WWW ("file:///" + file);
www.LoadImageIntoTexture (frontTexture);
CreateMaterial (frontTexture, frontObject);
}
}
if (GUILayout.Button ("Add Texture to back of Card"))
{
side = "back";
if (cardTypeToolbarInt == 0)
{
cardName = "Play_Play_" + seriesText + "_" + copy.ToString () + "_" + index.ToString () + "_" + side;
}
// camera.SelectCamera (1);
string file = EditorUtility.OpenFilePanel ("Hello", "", "png");
if (file.Length != 0)
{
WWW www = new WWW ("file:///" + file);
www.LoadImageIntoTexture (backTexture);
CreateMaterial (backTexture, backObject);
}
}
if (GUILayout.Button ("Save Card too Deck")) {
index++;
if (repeat == true) {
copy++;
} else {
copy = 0;
}
if (int.TryParse (randomNumberText, out randomNumber) == true && randomNumberText != "") {
aCard.RandomNumber = int.Parse (randomNumberText);
//aCard.name = cardName;
aCard.Colomn1Header = colomn1Text [0];
aCard.Colomn2Header = colomn2Text [0];
aCard.Colomn3Header = colomn3Text [0];
for (int i = 1; i <= 10; ++i) {
int.TryParse (colomn1Text [i], out aCard.Colomn1Values [i - 1]);
aCard.Colomn2Values [i - 1] = colomn2Text [i];
int.TryParse (colomn3Text [i], out aCard.Colomn3Values [i - 1]);
}
if (deckCount == oldDeckCount) {
deckCount += 1;
holdingCard = GameObject.Instantiate (card, new Vector3 (1.00f, 1.00f, deckCount * 0.05f), Quaternion.identity) as GameObject;
if (side == "front") {
cardName = cardName.Remove (cardName.Length - 6);
} else if (side == "back") {
cardName = cardName.Remove (cardName.Length - 5);
}
holdingCard.name = cardName;
var prefab = PrefabUtility.CreateEmptyPrefab ("Assets/Prefabs" + holdingCard.name + ".prefab");
PrefabUtility.ReplacePrefab (holdingCard, prefab);
side = "front";
oldDeckCount += 1;
}
} else {
EditorUtility.DisplayDialog ("Enter numbers Only.", "Please enter a interger value as random number.", "OK");
}
}
You have one texture reference (textureHolder) which you use for both the front and back. So when one changes, the other one naturally changes as well. You should separate them and use individual textures for each side.
Alright I added another texture called backTexture and placed it in the code for the button that loads the texture. But now the texture doesn't load anything. I missing something?
You're not making your code easy to understand by having the formatting all over the place. Problems are easier to detect when presented properly
Cannot help you without you showing us what you specifically are doing. So please update your code here with your new code. Also format it properly.
Alright I Updated and edited my code after trying a few things.
I stated the current problem but simple question is if the name of the children of the object are the same and the children hold the materials/textures does that mean I have to rename the children objects because the name is the same all around?
Your answer
Follow this Question
Related Questions
Making objects invisible 0 Answers
How to make dynamically texturable object model? 0 Answers
How to add texture to Blender objects properly? 2 Answers
Having wrong texture on my model? 1 Answer