Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Zastion · Aug 03, 2016 at 11:16 PM · meshmaterialsimulation

How do I change the MATERIAL of this mesh when i press T

So I have this

using UnityEngine; using System.Collections;

// Change renderer's material each changeInterval // seconds from the material array defined in the inspector. public class ExampleClass : MonoBehaviour { public Material[] materials; public float changeInterval = 0.33F; public Renderer rend;

 void Start() {
     rend = GetComponent<Renderer>();
     rend.enabled = true;
 }
 
 void Update() {
     if (materials.Length == 0)
         return;
     
     // we want this material index now
     int index = Mathf.FloorToInt(Time.time / changeInterval);
     
     // take a modulo with materials count so that animation repeats
     index = index % materials.Length;
     
     // assign it to the renderer
     rend.sharedMaterial = materials[index];
 }

} and it does what i want but it is on a timer so i tried to edit it but it didn't work I am horrible at C# and almost slightly decent at Javascript . I want it to switch between Materials NOT textures i need them to be materials drag and drop in the inspector and switch through them when i press "T" or "t" you know what I would also be happy with E R Y U I O P L K J H G V B N M any ideas .

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 itsharshdeep · Aug 04, 2016 at 12:39 PM

 public Material[] materials; 
     public float changeInterval = 0.33F;
     public Renderer rend;
     public Material additionalSingleMaterial;
 
  void Start() {
         rend = GetComponent<Renderer>();
         rend.enabled = true;
     }
 
     void Update() {
         // Untill T is pressed it will change/swtich b/w multiple materials : 
         if (Input.GetKey (KeyCode.T)) {
             if (materials.Length == 0)
                 return;
 
             // we want this material index now
             int index = Mathf.FloorToInt (Time.time / changeInterval);
 
             // take a modulo with materials count so that animation repeats
             index = index % materials.Length;
 
             // assign it to the renderer
             rend.sharedMaterial = materials [index];
         }
 
         // if you want to chage material only once then following code 
         if (Input.GetKey (KeyCode.P)) {
             rend.sharedMaterial = additionalSingleMaterial;
         }
 
     }
 

Attach script may help you in what you want to achieve. PLease let me know if you need any thing else in script. JavaScript version:

 #pragma strict
 
 
     var  materials : Material[]; 
     var changeInterval : float;
     var  rend : Renderer;
     var  additionalSingleMaterial : Material;
     var  index: int; 
 
 function Start () {
         rend = gameObject.GetComponent(Renderer);
         rend.enabled = true;
 }
 
 function Update () {
     // Untill T is pressed it will change/swtich b/w multiple materials : 
         if (Input.GetKey (KeyCode.T)) {
             if (materials.Length == 0){
                 return;
                 }
             // we want this material index now
             index = Mathf.FloorToInt (Time.time / changeInterval);
 
             // take a modulo with materials count so that animation repeats
             index = index % materials.Length;
 
             // assign it to the renderer
             rend.sharedMaterial = materials [index];
         }
 
         // if you want to chage material only once then following code 
         if (Input.GetKey (KeyCode.P)) {
             rend.sharedMaterial = additionalSingleMaterial;
         }
 }
 

 


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
avatar image
0

Answer by Zastion · Aug 04, 2016 at 10:18 PM

Okay thanks for the help ! exactly what i wanted ! but one question why does it go through the index randomly?(not a complaint) @ itsharshdeep

Comment
Add comment · Show 2 · 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 itsharshdeep · Aug 05, 2016 at 04:51 AM 0
Share

mm... First of all next time you need to post this response as a comment on the respective answer ins$$anonymous$$d of the adding a new answer.. because this is response to any answer not a answer..

avatar image itsharshdeep · Aug 05, 2016 at 05:52 AM 0
Share

I don't think so that it is going random.. I checked by putting log.. It is already in sequence ...

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

69 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do you draw a sprite from an atlas on to a mesh? 1 Answer

How to create a 3D Hemisphere? (Model of half a sphere) 0 Answers

Convert sub-mesh into a "single" mesh 0 Answers

Changing Prefab Meshes 0 Answers

Applying material to models exported from Blender 0 Answers


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