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 Erik-256 · Jun 20, 2013 at 08:47 PM · shaderraycastmaterials

Selecting Object. Changing material via script.

Here I am trying to use Unity for the first time. I Managed to make a simple FPS level and to make an object that, with the Raycast thingy, notices when I'm looking at it from reasonably near. Now I would like for it to be 'selected' when I look at it that way, that is, I crated another material with an outline and I would like that the object's material change when I look at it. By copying the Raycast code I had on a script on the player and pasting it in a script on the object I created this:

 Shader shader1 = Shader.Find ("Toon/Lighted");
     Shader shader2 = Shader.Find ("Toon/Lighted Outline");
             RaycastHit hit;
     Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width/2, Screen.height/2, 0));
     renderer.material.shader = shader1;
     
     
     if(Physics.Raycast(ray, out hit, 2))
     {
         if(hit.collider.tag == "Computer")
         {
             
             //Change material
             renderer.material.shader = shader2;
             
     
             if( Input.GetButton ("Fire1") ) {
                 //respawn
                 renderer.material.shader = shader1;
                 
             
             
             }
         }
         
     }

Since the camera is a child of the player it should work, but (even though it doesn't give me any errors) it doesn't, the material doesn't change. I know that there's a simpler function that should allow objects to know when the mouse overs over them, but it's possible to set a distance from the object for that to work? And it even works with FPS?

Thanks for your help.

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 moonstruck · Jun 21, 2013 at 07:43 AM

You don't want to change the shader, because when you do this Unity is most likely to copy the old parameters to a new shader. Instead add 2 public variables with materials, create them and assign in the inspector and change the material (renderer.material = yourMaterial).

Comment
Add comment · Show 5 · 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 Erik-256 · Jun 21, 2013 at 09:57 AM 0
Share

I think I got it, but I'm not sure about the kind of variable I'm supposed to use for materials.

avatar image AlucardJay · Jun 21, 2013 at 09:58 AM 0
Share
 var your$$anonymous$$aterial : $$anonymous$$aterial; // uJS

  • http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$aterial.html

  • http://docs.unity3d.com/Documentation/ScriptReference/Renderer-material.html

Edit :

 public $$anonymous$$aterial your$$anonymous$$aterial; // C#

If you look at the Unity Scripting Reference, on the right above the script example, there is a drop-down menu, by default set to JavaScript. Click on this, then select C# to see the example in C#

avatar image Erik-256 · Jun 21, 2013 at 10:22 AM 0
Share

I'm working with C#, that code doesn't work and in the documentation doesn't seem to be clear enough about how I'm supposed to do it in C#. Somebody have an idea?

avatar image moonstruck · Jun 21, 2013 at 10:31 AM 0
Share

public $$anonymous$$aterial your$$anonymous$$aterial;

That is how you can declare variables in C#. And don't forget to assign it in the inspector.

avatar image Erik-256 · Jun 21, 2013 at 10:46 AM 0
Share

I can't bloody believe it. I wrote "material" without the capital $$anonymous$$. Thank you, now it works perfectly. I think it's about time to make it a little bit neater.

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

16 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

Related Questions

Rule Tile which changes shader parameters 0 Answers

Changing material at runtime of a meshrenderer won't work 3 Answers

Raycasting in script suddenly stopped working 1 Answer

How can I add a color, smoothness and metallic property to this shader? 1 Answer

Need help with URP Lit Shader 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