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
1
Question by richardzzzarnold · Dec 10, 2010 at 11:31 AM · guimaterialorderelement

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)];



}

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 4 · 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
avatar image richardzzzarnold · Dec 10, 2010 at 01:22 PM 0
Share

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] ?

avatar image richardzzzarnold · Dec 10, 2010 at 01:26 PM 0
Share

Now, ins$$anonymous$$d of the first element changing, none of them do...any idea what I am doing wrong ?

avatar image richardzzzarnold · Dec 10, 2010 at 01:54 PM 0
Share

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 ?

avatar image Novodantis 1 · Dec 10, 2010 at 01:58 PM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Show 3 · 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
avatar image Novodantis 1 · Dec 10, 2010 at 03:50 PM 0
Share

Strange... does it work with any other material elements? [0,1,2]?

avatar image richardzzzarnold · Dec 10, 2010 at 03:54 PM 0
Share

nope, i tried them all

avatar image richardzzzarnold · Dec 10, 2010 at 04:04 PM 0
Share

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.

avatar image
0

Answer by pateljay · Mar 23, 2012 at 08:03 PM

I have the same problem...Please help!

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

1 Person is following this question.

avatar image

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


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