• 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
1

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

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

Raycast and RaycastHit failure 2 Answers

Ray Casting - Trigger function 1 Answer

Play ParticleSystem on RayCastHit Object 1 Answer

See what object I hit in raycast 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges