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 Thomas-Hawk · Dec 20, 2016 at 09:42 AM · raycastmaterialcolorrendererelse

Storing Renderer in a variable not working!

I have this in Update:

             RaycastHit hit;
             if (Physics.Raycast (transform.position, transform.forward, out hit, 80f) && !hit.transform.gameObject.CompareTag ("terrain")) {
                 Renderer rend = hit.transform.GetComponent<Renderer> ();
                 Renderer rendholder = hit.transform.GetComponent<Renderer> ();
                 rendholder.material.shader = hit.transform.gameObject.GetComponent<Renderer> ().material.shader;
                 rendeything = rendholder;
                 hit.transform.gameObject.GetComponent<Renderer> ().material.shader = Shader.Find ("Transparent/Diffuse");
                 Color tempColor = rend.material.color;
                 tempColor.a = 0.5f;
                 rend.material.color = Color.Lerp(rend.material.color, tempColor, 5f);
                 Debug.DrawLine (transform.position, hit.point);
                 hideything = hit.transform.gameObject;
                 Invoke ("Treset", 6f);
             }


And this is its own function:

      public void Treset(){
             hideything.GetComponent<Renderer>().material.shader = rendeything.material.shader;
             hideything.GetComponent<Renderer>().material.color = rendeything.material.color;
             Debug.Log ("Reset mats on" + hideything.name.ToString ());
                     }
 
 


The idea is, after 6 seconds, set the hideything's shader and color back to what it was when the raycast function logged it, before changing it.

Honestly, I'm only trying to do it after a timer because I don't know how to write "If the object is no longer being hit, return it to its original shader and color.

The debug log goes off, way too many times, but the objects are still transparent, Would love if someone let me know what I'm doing wrong!

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
0
Best Answer

Answer by AurimasBlazulionis · Dec 20, 2016 at 10:20 AM

It is because when you call material.color, you edit the copy of that material, not the material itself. To deal with that, you have to either:

A) read the material to a temporary variable, edit the temporary material and then set it back. Be careful since it can be hard on memory if you change these a lot because it copies the material each time.

B) use sharedMaterial instead of material. This allows you to edit the actual material, not the copy. But be careful, since it edits that material and the changes persist even after stopping the game in the editor.

The compromise would be to duplicate the material using something like theRenderer.material = theRenderer.material in the start and then use sharedMaterial in your code. This would prevent from accidental edits in the editor and it will not be hard on memory.

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 Thomas-Hawk · Dec 20, 2016 at 04:57 PM 0
Share

TYS$$anonymous$$, Love answers that $$anonymous$$ch me things!

The thing though, isn't that what I am doing with Rendholder in my script? Im then sending Rendholder to Treset through rendeything, but it's still not working, as far as changing the material back.

I'm a little confused at this: theRenderer.material = the theRenderer.material, is that the syntax for it?

avatar image AurimasBlazulionis Thomas-Hawk · Dec 20, 2016 at 11:55 PM 0
Share

oh no, that is not the syntax, I made a mistake.

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

110 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

Related Questions

material color not changing 1 Answer

Changing a material's colors anywhere else besides the Update function has no effect,Changing Material's color works on Update but not on another method 0 Answers

2 same Scripts w/ GetComponent : one works, one doesn't 2 Answers

How to Disable or hide a material in Unity. 0 Answers

Material darker in inspector then scene? 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