I have a project where initially there will be no Game Objects in the scene hierarchy other than a control script. I have several prefabs that share a script whose purpose is to switch the main diffuse texture. The script has 8 slots to define the textures to use. When I use this script to switch the texture the prefab object that I am updating switches to ‘Type mismatch’ while the game is running and then the materials that were assigned to the prefab become ‘Missing (Material)’ when the game stops running. I am on a Mac running 3.5.7f6. I do not see the texture change in the game view. However, if the object is selected then the small preview window does show the changes to the texture. I am totally confused.
it would be more useful to see the relevant part of the script than have it described…
bool updateTexture(int id){
Debug.Log (TAG + "updateTexture to " + id + " of " + this.textures.Count);
//count++;
if (id<0 || id>textures.Count){
return false;
}
Texture texture = textures[id];
if (texture == null){
Debug.Log (TAG + “updateTexture texure is null”);
}else{
Debug.Log (TAG + "updateTexture to " + texture.ToString());
Transform tran = gameObject.transform.GetChild (1);
if (tran==null){
Debug.Log (TAG + “updateTexture child not found”);
}else{
GameObject obj = tran.gameObject;
if (obj==null){
Debug.Log (TAG + “changeTexture gameObject not found”);
}else{
MeshRenderer currentRenderer = obj.GetComponent();
if (currentRenderer != null){
currentRenderer.material.mainTexture = texture;
Debug.Log (TAG + “updateTexture " + currentRenderer.materials.Length + " materials amended”);
texId = newTexId;
}else{
Debug.Log (TAG + “updateTexture no MeshRenderer”);
return false;
}
return true;
}
}
}
return false;
}
Sorry about the formatting - it seems to trash the identing
Problem appears to be the way I am calling a script attached to a prefab that has been created using Instantiate. More research needed about the scope of scripts using Instantiation. If I restrict all my code to the script for a prefab it all works fine. If instead I try to call the scripts in the instantiated prefabs using a master control script, I get the issues I have been having problems with.
Click the “Go Advanced” button in the bottom ricght and then click the “Wrap code tags around selected text” button.
bool updateTexture(int id){
Debug.Log (TAG + "updateTexture to " + id + " of " + this.textures.Count);
//count++;
if (id<0 || id>textures.Count){
return false;
}
Texture texture = textures[id];
if (texture == null){
Debug.Log (TAG + "updateTexture texure is null");
}else{
Debug.Log (TAG + "updateTexture to " + texture.ToString());
Transform tran = gameObject.transform.GetChild (1);
if (tran==null){
Debug.Log (TAG + "updateTexture child not found");
}else{
GameObject obj = tran.gameObject;
if (obj==null){
Debug.Log (TAG + "changeTexture gameObject not found");
}else{
MeshRenderer currentRenderer = obj.GetComponent<MeshRenderer>();
if (currentRenderer != null){
currentRenderer.material.mainTexture = texture;
Debug.Log (TAG + "updateTexture " + currentRenderer.materials.Length + " materials amended");
texId = newTexId;
}else{
Debug.Log (TAG + "updateTexture no MeshRenderer");
return false;
}
return true;
}
}
}
return false;
}
Fixed this. My issue was the way I was Instantiating the prefab not the texture switching code.