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 Rembo4Fight · Dec 29, 2017 at 08:52 AM · shader programmingscriptingproblempropertymaterialpropertyblock

! Change Material Value !

I have the script, where i changing the meterial on child objects. And I need to change the value of the material "Threshold" parametr to be like looping from 0 to 1 and backwords. Please try to help in this one Right now its just getting the value of threshold, not changing it

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MatSwitch : MonoBehaviour {
 
     public Material mainMat;
     public Material newMat;
     public Renderer childrens;
 
     public Shader invert;
 
     public float min = 0.0f;
     public float max = 1.0f;
 
     static float t = 0.0f;
 
     public float thres;
 
     public bool isMatNew;
 
     void Start()
     {
         isMatNew = false;
 
         thres = newMat.GetFloat("_Threshold");
 
     }
 
         void Update () {
         Renderer[] childrens;
         childrens = GetComponentsInChildren<Renderer> ();
 
 
 
         if (isMatNew == true) {
             
             thres = Mathf.Lerp (min, max, t);
             t += 0.5f * Time.deltaTime;
 
             foreach (Renderer rend in childrens) {
                 var mats = new Material[rend.materials.Length];
                 for (var j = 0; j < rend.materials.Length; j++) {
                     mats [j] = newMat;
                 }
                 rend.materials = mats;
             }
         }
         if (isMatNew == false) 
         {
             foreach (Renderer rend in childrens) {
                 var mainMats = new Material[rend.materials.Length];
                 for (var k = 0; k < rend.materials.Length; k++) {
                     mainMats [k] = mainMat;
                 }
                 rend.materials = mainMats;
             }
         }
 
         if (t > 1.0f) 
         {
             float temp = max;
             max = min;
             min = max;
             t = 0.0f;
         }
     }
 } 
 
 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Dragate · Dec 29, 2017 at 09:14 AM

You forgot to apply your value after changing it

 newMat.SetFloat("_Threshold", thres); 

Take a look here: https://docs.unity3d.com/ScriptReference/Mathf.PingPong.html
Does exactly what you want and will simplify your code.

Comment
Add comment · Show 10 · 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 Rembo4Fight · Dec 29, 2017 at 09:26 AM 0
Share

I have no "SetFloat" but GetFloat function :) And when im trying to add thres in brackets it makes the error : "No overload for method "getfloat" takes 2 argument :(

avatar image Dragate Rembo4Fight · Dec 29, 2017 at 09:34 AM 0
Share

You need to add the SetFloat() function. Leave GeFloat() as you had it.

 thres = $$anonymous$$athf.Lerp ($$anonymous$$, max, t);
 t += 0.5f * Time.deltaTime;
 new$$anonymous$$at.SetFloat("_Threshold", thres);
avatar image Rembo4Fight · Dec 29, 2017 at 09:41 AM 0
Share

Wow, that is really working!) Thank you Right now i just need to add pingpong ins$$anonymous$$d of lerping

avatar image Rembo4Fight · Dec 29, 2017 at 09:48 AM 0
Share

I have tried ping pong And i dont know whats wrong It goes to 1f and after its just showing "NaN" and not turning back to 0 I type like this ins$$anonymous$$d of mathf.lerp :

 thres = $$anonymous$$athf.PingPong(t, max);
avatar image Dragate Rembo4Fight · Dec 29, 2017 at 10:01 AM 0
Share

I cannot explain why it shows up "NaN". Delete/Comment the following code you have:

 if (t > 1.0f) {
     float temp = max;
     max = $$anonymous$$;
     $$anonymous$$ = max;
     t = 0.0f;
 }

and replace

 thres = $$anonymous$$athf.PingPong(t, max);
 t += 0.5f * Time.deltaTime;
 new$$anonymous$$at.SetFloat("_Threshold", thres);

with

 thres = $$anonymous$$athf.PingPong(Time.time * .5f, max);
 new$$anonymous$$at.SetFloat("_Threshold", thres);

Is the behaviour still weird?

avatar image Rembo4Fight Dragate · Dec 29, 2017 at 10:05 AM 0
Share

It's working perfectly, thank you :) Can you explain one more thing for me? How to make it change the value between exact numbers And how to to make transition of it slower?

Show more comments

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

121 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 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

Change Material Parameters Value?? 0 Answers

Animated Icon in Unity Dropdown Menu 0 Answers

Try part of C# not being Read 1 Answer

Pathfindingsystem Script for Waypoints doesn't work. 1 Answer

UnityCar - Controlling Speed with a duration? 2 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