- Home /
Floating GUI button to change correct material of character
I have a script which keeps a floating GUI button over a character. Whenever the button is touched it activates a change in material.The only problem is that if my character is imported as a single mesh with five different materials I can only get the button to affect material element 0. If I want it to affect material element 3, how do I get it to do that ? I am not sure how to change the element order.
My script is like this:
var materials : Material[];
var buttonTexture : Texture; //The texture to use on the button var offset = Vector3.up; //World space offset. 1 unit above object by default var clampToScreen = false; //Will the label be visible if object is off screen var clampBorderSize = .05; //screen space to leave when clamped var useMainCamera = true; //Use the camera tagged MainCamera var cameraToUse : Camera; //Camera to use if useMainCamera is false private var cam : Camera; //The camera we're using private var screenPos : Vector3; //The screen position of the button
function Start () { if(useMainCamera) cam = Camera.main; else cam = cameraToUse; }
function Update () { if(clampToScreen) { var relativePosition = cam.transform.InverseTransformPoint(transform.position); relativePosition.z = Mathf.Max(relativePosition.z, 1.0); screenPos= cam.WorldToScreenPoint(cam.transform.TransformPoint(relativePosition + offset)); screenPos= Vector3(Mathf.Clamp(screenPos.x, clampBorderSize,Screen.width-(clampBorderSize+buttonTexture.width)), Mathf.Clamp(screenPos.y, clampBorderSize,Screen.height-(clampBorderSize+buttonTexture.height)), screenPos.z); } else screenPos= cam.WorldToScreenPoint(transform.position + offset); }
function OnGUI() { if(GUI.Button(Rect(screenPos.x,Screen.height - screenPos.y,buttonTexture.width,buttonTexture.height),buttonTexture))
renderer.material = materials[Random.Range(0,materials.length)];
}
Answer by Novodantis 1 · Dec 10, 2010 at 12:26 PM
You can use:
renderer.materials[i]
where i is the number material you want to affect. You can use a loop with renderer.materials.length to go through all materials.
Thanks Novodantis, I just tried this but it doesnt seem to do anything. Is it only in the OnGUI function where you have to change to renderer.materials[3] ?
Now, ins$$anonymous$$d of the first element changing, none of them do...any idea what I am doing wrong ?
For some reason the material elements switch to instances when clicked. In the material list in the Skinned $$anonymous$$esh Renderer in the inspector, when I click on the GUI button, it comes up ( instanced ) Next to the material. What the heck is that ?
In the code you've posted above, change that last line (render.material equals) to: renderer.materials[2] = materials.... and it should change the third material to the one you're choosing with Random.Range(). If you're still having problems, post additional info on the behaviour you're going for and I'll try and help.
Answer by richardzzzarnold · Dec 10, 2010 at 02:48 PM
Yeah, I dont know, its weird ... I can see that it should be a simple fix but it is not working for some reason. I have done it just like you say replacing renderer.material with renderer.materials[3] and it doesnt change the material. I can see that it is registering the click though.The original script works fine on a character which has the changeable skin as element 0. But the next character, the skin has come in as element 3 and the eyeballs are element 0, so it make sense to change the script in this way, but it is not doing anything for some reason. In the inspector with the original you can see when the individual materials are changing , but in this case, with the renderer.materials[3], they dont change, though I can see that they are reading the click, as "instanced "comes up next to the material when clicked.weird.
Strange... does it work with any other material elements? [0,1,2]?
i just tried this with a simple sphere with four different textures. with the first script, element 0 changes ok, but with the second script ( with the renderer.materials[i] ) , nothing.
Your answer
Follow this Question
Related Questions
How to check if player is hovering mouse over a GUI Box 3 Answers
How to remove a GUI element when button is pressed C# 1 Answer
Change material per element 1 Answer
Simple GUI Question 1 Answer
Offset detail texture in c# 1 Answer