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 /
avatar image
0
Question by ramymaher · Dec 15, 2015 at 09:32 PM · gameobjectraycastmaterialraycasthitgameobject.find

How do I change the raycasthit material back?

I am dealing with raycasting in the middle of the screen. I want to change the material of the hit object only when the raycast is hitting it.

I created an empty gameobject (dummie) to store the hit object in it.

I then changed the color when the raycast hits the object. The problem is that it changes the color of all the objects touched if the raycast is always hitting something. I need to change the color of the material back if I am not hovering over it.

I know exactly what causes the problem: the gameObject stored in the "obj" is left with its new material and the new one that replaces it has the new material. I need to access all the materials with the same new material and only affect the one that I am currently hovering over.

Here is the part of the code which has the problem:

public Material newMtl; public Material oldMtl;

void update(){ if(Physics.Raycast (vct, fwd, out hit)){ obj = hit.collider.gameObject; obj.GetComponent().material = newMtl; }else{ obj.GetComponent().material= oldMtl; } }

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
2

Answer by OncaLupe · Dec 17, 2015 at 06:26 AM

Please format your code by highlighting it and clicking the '101010' button, as it says in the user guide on the right. Makes it much easier to read, as well as not losing stuff inside angle brackets.

After the raycast, but before you set the obj value, check to see if it's different than the hit object. If it's different, reset the obj's material, then set obj to the new hit target. This also avoids it constantly trying to set the material of the object it's pointed at.

You also need to make sure obj is not null before changing its value in the else section or you'll get a Null Reference Exception right from the start if not pointing at anything.

 using UnityEngine;
 using System.Collections;
 
 public class TestScript : MonoBehaviour
 {
     public Vector3 vct;
     public Vector3 fwd;
     RaycastHit hit;
 
     GameObject obj;
     Renderer objRenderer;
     public Material newMtl;
     Material oldMtl;
 
     void Update()
     {
         if(Physics.Raycast (vct, fwd, out hit))
         {
             if(obj != hit.collider.gameObject)//If we're not pointing at the previous target
             {
                 if(obj != null)//If previous target is set, reset its material
                 {
                     objRenderer.material = oldMtl;
                 }
                 
                 obj = hit.collider.gameObject;//Store reference of target to a variable
                 objRenderer = obj.GetComponent<Renderer>();//Get targets Renderer
                 oldMtl = objRenderer.material;//Store targets current material
                 objRenderer.material = newMtl;//Set target to new material
             }
         }else{//If we're not pointing at anything
             if(obj != null)
             {
                 objRenderer.material = oldMtl;//Reset targets material
                 obj = null;//Clear reference
             }
         }
     }
 }

I also have it store the Renderer of the target to reduce GetComponent() usage, as it's a slow operation. Finally, I have it set oldMtl to the targets material, so targets can have different materials if you want.

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Store previous gameObject that raycast had hit. (Or store any previous value in Update for that matter) 1 Answer

Detect Object that are hit by a ray 3 Answers

Get collider from raycast 4 Answers

How to make my raycast also hit deactive objects WITHOUT showing the object? 1 Answer

Shoot off multiple raycasts from 1 object? 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