- Home /
Texture change on multi material character
Good day! I'm trying to trigger different face textures using animation events. I've tried using different variables, from gameobjects (the player) to materials. I've checked several threads on Unity Answers, but all answers are scripts that point to the mainTexture, which it does anyways by default..
I mean, the script I have now works, as far as triggering texture changes from the animation. But it changes the wrong texture!
Any ideas of how to solve this would be appreciated, thank you!
var mat : Material;
as well as Materials[]; and then call Materials[2]; in the script.
none of those work, and I get unassigned bugs from them.
I have also tried making a custom shader for the part of the character I want to change the texture of, and changing the standard Properties _MainTex to "_FaceTex" and then call that in the script, but that doesn't work either!
this is what I have right now, what it does is changing the texture of the first texture it can find, rather than the one I'd want it to change.
var player : GameObject;
var neutral : UnityEngine.Texture;
var angry : UnityEngine.Texture;
var scream : UnityEngine.Texture;
function FaceNeutral () {
player.renderer.material.mainTexture = neutral;
}
function FaceScream () {
player.renderer.material.mainTexture = scream;
}
I still haven't solved this problem.
http://www.youtube.com/watch?v=tZ8wIy-cpYo
Here's a video describing the problem a bit further.
Hoping that somebody can help me with this one! :) Thanks!
Answer by richard3d · Dec 04, 2012 at 09:37 PM
I think this is what you are looking for http://docs.unity3d.com/Documentation/ScriptReference/Material.GetTexture.html
This is how you can access named textures from the material. I am assuming your shader or material has some texture samplers that are named faceTexture, bodyTexture or something along those lines and maybe you just want to change the face texture. Hope this helps - R
thank you for the reply!
yeah I've checked ou the script ref and other unity answers, but they all seem to focus on the $$anonymous$$ainTex stuff. the example script for the GetTexture is horrible and doesn't apply to my problem. I have tried their script straight up, and it works if I do .GetTexture ("$$anonymous$$ainTex"), it tells the console what texture is assigned and what not. But if I change it to _FaceTex, it tells me there's no texture connected.
As far as I understand, the whole _NameTex is the name of the property in the shader, which in my case is _FaceTex, so I haven't overlooked that part..
Even trying "renderer.material.SetTexture("FaceTex", texture);" only works if I write $$anonymous$$ainTex.
HAS to be something related to the _FaceTex property I have in my shader I assume? it doesn't differ that much from a normal unlit shader though, just that property name..
Have you tried removing the underscore before FaceTex? I believe the underscored names are only for referencing pre-defined texture units like the Cubemap etc. Unless the name in your shader literally says _FaceTex also.
Also, if you wouldn't $$anonymous$$d pasting your shader code it could give some more insight as well
Thank you again for the comments! I tried removing the underscore before FaceTex, didn't work. No underscore before $$anonymous$$ainTex didn't work either, but as soon as I add the underscore _$$anonymous$$ainTex, the hair starts changing textures again.
The video I posted can be viewed in glorious 1080p so you can read the shaders and javascript files in there. But here is the shader file, used for the material which has the face textures on it;
The javascript attached to the player, looks like this; (currently set to $$anonymous$$ainTex, but this is the part I keep changing back and forth between $$anonymous$$ainTex and _FaceTex)
And is used as a trigger in the animation. For later I want him to look angry or whatever, at certain parts of fight animations.
Hey thanks for posting your code, I was able to make it run without any major changes so it must be something else that is the root of your problem. I did the following to make it work: create a blank material and assign the shader to it. Create a cube gameobject and assign the material and the script to it. I modified the script a bit so it wouldnt have to point to a specific gameobject (since in this case it is already attached to the cube). I also added the Update function to the script and just had it call FaceNeutral the whole time (these were the only changes). So take a look at your setup again and maybe use some debug logging to make sure your functions are getting called etc. Good luck! Also if you wouldn't $$anonymous$$d marking this as answered it would be awesome.
Your answer
Follow this Question
Related Questions
Changing GameObject texture? 4 Answers
Texture an Object at Runtime, work through help needed. 0 Answers
How can i change detail texture? 1 Answer
Change Material of an Object. 5 Answers
referring to a particular material 0 Answers