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
0
Question by Master143 · Dec 10, 2012 at 10:09 AM · renderingmaterialdynamic

Material Selection problem for Rendering

alt text

Hi I am new to unity3d. i am just trying to change the Material of wall at run time.... So i just import the model of house form Google Sketch up.... Which have different material in one object...As you can see in inspector view......

my problem is that when ever i click the next button ">>" it change the first material of the object....As shown in image, Red circle in inspector view is representing that the first material is changing and in game view there is next button under the red circle....

Some how if i get the ref of other elements then that will be great for me.... Code is given below....

public class Material_GUI : MonoBehaviour {

public Material[] mats; public GameObject go; private int index = 0; // Use this for initialization void Start () { go.renderer.material= mats[index]; } // Update is called once per frame void Update () { }//end of if update

void OnGUI(){ GUILayout.BeginArea(new Rect(Screen.width/2-100,Screen.height-60,200,50)); GUI.Box (new Rect(10,10,190,40),""); GUI.Label(new Rect(62,20,100,20),"Wall Testing"+(index +1)); if(GUI.Button (new Rect(15,15,30,30),"<<")){ index--; if(index<0){ index = mats.Length - 1; } go.renderer.material = mats[index]; } if(GUI.Button (new Rect(165,15,30,30),">>")){ index++; if(index > mats.Length -1){ index = 0; } go.renderer.material = mats[index]; } GUILayout.EndArea(); }//end of OnGUI Funcation

}

0.png (458.0 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Maulik2208 · Dec 10, 2012 at 10:38 AM

Can you explain what are you upto???? Beacuse according to me if you want to change Texture then you will need some kind of modification in your script so please provide your feed back what do you want???? For changing the look of the wall you can simply change the texture and you are all set to go and if you are trying to do the same then just use Gameobject.renderer.material.Maintexture = texturename; and Enjoy........For more info please refer to this link link text

Comment
Add comment · Show 1 · 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 Master143 · Dec 10, 2012 at 11:51 AM 0
Share

Thanks man... But my problem is that there are different room in my house model and 15 materials of texture are there which is denoted by element 0 to element 14 as shown in image....

each element is representing texture(material)of different room.... i am using the above code for rendering... it is just changing my first element material.....which is located at element 0,,,,,my entire model is in mesh form.....

so my problem is that if some how i am able to get 2 or 3 element texture in code.......

so it will great for me.... i want to change the marital(texture) of the walls at run time....

i want to know how i can assess the element 2 or 3 at run time... by scripting....

Thank you...!!!

avatar image
0

Answer by Hardcore Games · Aug 15, 2014 at 03:23 PM

Well my friend. I have the same problem you have. So far I can tell you this:

         if (GUI.Button(new Rect(120,250,200,40),'Ladrillos Crudos')){
             theObject = GameObject.Find(casterScript.theThing);
             mats = theObject.renderer.materials;
             for (mat = 0 ; mat < theObject.renderer.materials.Length -1 ; mat++){
                 mats[mat] = materialPared_3;
             }
             theObject.Renderer.materials = mats;

/I've Also tryied theObject.cachedRenderer.materials = mats; with no change /

         }

As you can figure out, the GUI.Button will trigger the material changing. casterScript is a RaycastHit returning the name of the mesh and the type of material stored in its Tag. What I've read is that you can only change different elements materials by refering to an array called "materialS". Then you'll need to go though this array changing the right element in the already declared material array (Declare lik this: var mats : Material[];) This code hasnt work for me so far. I hope this help you in your research. Please, let me know I you found final solution. Thnx

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

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why do shadows still show up with "receive shadows" unchecked? 0 Answers

Point lights break dynamic batching even when using Unlit 1 Answer

change material of one prefab 2 Answers

Texture Stretching Not Tiling (HDRP) 0 Answers

different material for multi camera 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