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 PISSDRANK8792 · Jul 31, 2013 at 06:12 PM · shadermouse click

Help doing mouse click changin shader and back to normal to a gameObject ꜛ!

i need help with a code, i need to change the shader of a obj who is "Vertex Lit" while is not selected and if you click on it the shader changes into "Transparent/Bumped Specular"; if you click in no obj but the camara background, everyone is "Vertex Lit" as it should be the normal.

this a script example in my proyect like should be with the shaders but with GUI.

this script is a component for a gameObject that if you click on it, the text you input in the string should be shown:

 public class Character : MonoBehaviour
 {
     public string myWords = string.Empty;
         
     private void Start () 
     {    
     
     }
     
     private void Update ()
     {
         
     }
     
     public string GetWords ()    
     {
         return myWords;
     }    
 }

and this goes for the camera allowing to click a obj, if you click on another obj and if you click on the camara background:

 public class UI_Demo : MonoBehaviour
 {
     private GameObject clicked = null;
     
     private void Start()
     {
     
     }
     
     private void Update ()
     {
         if (Input.GetMouseButtonDown(0)) 
         {
             RaycastHit hit;
             
             if (Physics.Raycast(Camera.mainCamera.ScreenPointToRay(Input.mousePosition), out hit))
             {                
                 if (hit.collider.gameObject.GetComponent<Character>() != null)
                     clicked = hit.collider.gameObject;
                 else
                     clicked = null;
             }
             else 
                 clicked = null;
         }
     }
     private void OnGUI()
     {
         if (clicked != null)
             GUI.Label(new Rect(15,15,200,25), clicked.GetComponent<Character>().GetWords());
         }
     }


can anyone help me please :( i don't know very much of C# and scripting you can be in the credits or teach me right here. ty if you are reading this coders! and sorry for my bad english..

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

Answer by HansMcCode · Jul 31, 2013 at 06:57 PM

Would you be looking for something like this?

 using UnityEngine;
 using System.Collections;
 
 public class ShaderChanger : MonoBehaviour
 {
     public Shader shader1;
     public Shader shader2;
 
     void Awake()
     {
          shader1 = Shader.Find("Diffuse");
          shader2= Shader.Find("Transparent/Diffuse");
     }
 
     void Update()
     {
         if (Input.GetButtonDown("Jump"))
         {
             if (renderer.material.shader == shader1)
                 renderer.material.shader = shader2;
             else
                 renderer.material.shader = shader1;
         }        
     }
 }
 


This should change the shader to whatever you'd like while the space bar is held down. Should be able to hook it into your click detection easily enough.

"renderer.material.shader = " is the important line that set the shader

Hope this helps :)

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 HansMcCode · Jul 31, 2013 at 07:09 PM 0
Share

Updated my code to match this, I recommend going through the script ref, incredibly useful. http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$aterial-shader.html

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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Material doesn't have a color property '_Color' 4 Answers

Card matching game 1 Answer

How to control the FarClipPlane of a reflection camera in Unity3d (4.3f1) 1 Answer

Glass Material 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