• 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 george_vasilchenko · Sep 28, 2014 at 02:55 AM · raycastmaterial

Material change problem

Hey guys, I have trouble changing material on my object. I cast a ray on an object, I set bool to true if the ray hits it and my LMB is pressed. While the bool true, I want to change a material of an object. It doesn't work for some reason. Can u help? Thanks!

using UnityEngine; using System.Collections;

public class RaycastSelection : MonoBehaviour {

 public GameObject roof;
 public GameObject body;
 public GameObject blades;
 public GameObject fabric;
 public GameObject door;
 public GameObject veranda;
 public GameObject stairs;
 public GameObject basement;
 public GameObject ground;

 public Material roofMat;
 public Material bodyMat;
 public Material bladesMat;
 public Material fabricMat;
 public Material doorMat;
 public Material verandaMat;
 public Material stairsMat;
 public Material basementMat;
 public Material groundMat;



 public Material glowMaterial;

 bool isSelected = true;

 Color beginSelectColor = new Vector4(0, 0, 0, 1);
 Color endSelectColor = new Vector4(1, 2, 0, 1);

 float selectColorDuration = 1.0f;
 float selectGlowStrenght = 2.0f;
 

 public bool roofIsSelected = false;
 public bool bodyIsSelected = false;
 public bool bladesRIsSelected = false;
 public bool fabricIsSelected = false;
 public bool doorIsSelected = false;
 public bool verandaIsSelected = false;
 public bool stairsIsSelected = false;
 public bool baseIsSelected = false;
 public bool groundIsSelected = false;


 void Update()
 {
     CheckThePartRayCast();

     if(roofIsSelected)roof.renderer.material.color = Color.red;
 }
 void CheckThePartRayCast()
 {
     Ray ray;
     RaycastHit hit;
     
     ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     
     if(Physics.Raycast(ray, out hit, 500.0f))
     {
         if(Input.GetMouseButtonDown(0) && hit.collider.name == "body")
         {
             bodyIsSelected = true;
             
             roofIsSelected       = false;
             bladesRIsSelected = false;
             fabricIsSelected  = false;
             doorIsSelected       = false;
             verandaIsSelected = false;
             stairsIsSelected  = false;
             baseIsSelected    = false;
             groundIsSelected  = false;
     
             Debug.Log("body");
         }
         if(Input.GetMouseButtonDown(0) && hit.collider.name == "roof")
         {
             roofIsSelected = true;
             
             bodyIsSelected       = false;
             bladesRIsSelected = false;
             fabricIsSelected  = false;
             doorIsSelected       = false;
             verandaIsSelected = false;
             stairsIsSelected  = false;
             baseIsSelected    = false;
             groundIsSelected  = false;
             Debug.Log("roof");
         }
         
         if(Input.GetMouseButtonDown(0) && hit.collider.name == "blades_rect")
         {
             bladesRIsSelected = true;
             
             bodyIsSelected       = false;
             roofIsSelected    = false;
             fabricIsSelected  = false;
             doorIsSelected       = false;
             verandaIsSelected = false;
             stairsIsSelected  = false;
             baseIsSelected    = false;
             groundIsSelected  = false;
             Debug.Log("blades_rect");
         }
         if(Input.GetMouseButtonDown(0) && hit.collider.name == "blades_fabric")
         {
             fabricIsSelected = true;
             
             bodyIsSelected       = false;
             roofIsSelected    = false;
             bladesRIsSelected = false;
             doorIsSelected       = false;
             verandaIsSelected = false;
             stairsIsSelected  = false;
             baseIsSelected    = false;
             groundIsSelected  = false;
             Debug.Log("blades_fabric");
         }
         if(Input.GetMouseButtonDown(0) && hit.collider.name == "door")
         {
             doorIsSelected = true;
             
             bodyIsSelected       = false;
             roofIsSelected    = false;
             bladesRIsSelected = false;
             fabricIsSelected  = false;
             verandaIsSelected = false;
             stairsIsSelected  = false;
             baseIsSelected    = false;
             groundIsSelected  = false;
             Debug.Log("door");
         }
         if(Input.GetMouseButtonDown(0) && hit.collider.name == "veranda")
         {
             verandaIsSelected = true;
             
             bodyIsSelected       = false;
             roofIsSelected    = false;
             bladesRIsSelected = false;
             fabricIsSelected  = false;
             doorIsSelected    = false;
             stairsIsSelected  = false;
             baseIsSelected    = false;
             groundIsSelected  = false;
             Debug.Log("veranda");
         }
         if(Input.GetMouseButtonDown(0) && hit.collider.name == "stairs")
         {
             stairsIsSelected = true;
             
             bodyIsSelected       = false;
             roofIsSelected    = false;
             bladesRIsSelected = false;
             fabricIsSelected  = false;
             doorIsSelected    = false;
             verandaIsSelected = false;
             baseIsSelected    = false;
             groundIsSelected  = false;
             Debug.Log("stairs");
         }
         if(Input.GetMouseButtonDown(0) && hit.collider.name == "base")
         {
             baseIsSelected = true;
             
             bodyIsSelected       = false;
             roofIsSelected    = false;
             bladesRIsSelected = false;
             fabricIsSelected  = false;
             doorIsSelected    = false;
             verandaIsSelected = false;
             stairsIsSelected  = false;
             groundIsSelected  = false;
             Debug.Log("base");
         }
         if(Input.GetMouseButtonDown(0) && hit.collider.name == "ground")
         {
             groundIsSelected = true;
             
             bodyIsSelected       = false;
             roofIsSelected    = false;
             bladesRIsSelected = false;
             fabricIsSelected  = false;
             doorIsSelected    = false;
             verandaIsSelected = false;
             stairsIsSelected  = false;
             baseIsSelected    = false;
             Debug.Log("ground");
         }
     }
 }

 void ChangeMaterialOnSelect(bool part, GameObject target, Material selectMat, Material backUpMat)
 {
     if(part)target.renderer.material = selectMat;
     if(!part)target.renderer.material = backUpMat;
 }
 void PingPongMaterial(GameObject selection, Color begin, Color end, float t, float s)
 {
     float lerp = Mathf.PingPong(Time.time, t);
     Color newColor = Color.Lerp(begin, end, lerp);
     
     selection.renderer.material.SetColor("_GlowColor", newColor);
     selection.renderer.material.SetFloat("_GlowStrength", s);

      
 }

}

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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

How to change colour of Game objects when hit by ray cast using array 0 Answers

Getting material index from raycast 2 Answers

How do I change the raycasthit material back? 1 Answer

Checking material on raycast 1 Answer

3D heatmap visualization on geometry 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