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
1
Question by LPGaming-Company · Jan 17, 2013 at 06:23 PM · raycastingrenderertargetting

Having problems changing the material of current target.

I've created a basic targeting system using RayCasting by using the Scripting Reference and I'm trying to set the target to a different color, currently this is working. Although, I would like to make it so that ONLY the current target has the color, which is "Red". When the target is De-Selected and another target is selected, I would like for it to go back to it's original color and turn the new target red. Not sure how to go about the reverting.

 using UnityEngine;
 using System.Collections;
 
 public class RaycastTargetting : MonoBehaviour {
     RaycastHit hit;
     Ray ray;
     public Transform target;
     
     void Update (){
           if (Input.GetButton("Fire1")){
             ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast (ray, out hit, 250)){
                   if (hit.transform.tag == "NPC"){
                     target = hit.transform;
                     ApplyTarget(target);
                   }
                 }
           }
     }
     
     void ApplyTarget(Transform theTarget) {
           theTarget.renderer.material.color = Color.red;
     }
 }
Comment
Add comment · Show 2
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 dorpeleg · Jan 17, 2013 at 06:27 PM 0
Share

If you could post sample of your code it would be easier to help you

avatar image LPGaming-Company · Jan 17, 2013 at 06:32 PM 0
Share

Updated it with the code, like I said it's just the basic from the scripting reference.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Bunny83 · Jan 17, 2013 at 06:50 PM

@Sillydan solution is correct, however it would be better to store the original sharedMaterial and assign that back. Otherwise the material will be duplicated and it will break dynamic batching.

I would do something like that:

 public class RaycastTargetting : MonoBehaviour
 {
     public Transform target = null;
     private Material m_OldMaterial = null;
     void Update ()
     {
         if (Input.GetButton("Fire1"))
         {
             RaycastHit hit;
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast (ray, out hit, 250))
             {
                 if (hit.transform.tag == "NPC")
                 {
                     SetTarget(hit.transform);
                 }
             }
         }
     }
 
     void SetTarget(Transform newTarget)
     {
         if (target != null && m_OldMaterial != null)
         {
             target.renderer.sharedMaterial = m_OldMaterial;
         }
         target = newTarget;
         if (target != null)
         {
             m_OldMaterial = target.renderer.sharedMaterial;
             target.renderer.material.color = Color.red;
         }
     }
 }
Comment
Add comment · Show 2 · 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 LPGaming-Company · Jan 17, 2013 at 07:05 PM 0
Share

Apparently I don't have permission to thumbs up, I'm going to go ahead and give this the correct answer because you showed some code, although on the line target.shared$$anonymous$$aterial it should be target.renderer.shared$$anonymous$$aterial Thanks for the help, I didn't really think about changing the value like that.

avatar image Bunny83 · Jan 17, 2013 at 10:13 PM 0
Share

Ohh, Yes a typo ;) I'll fix it.

avatar image
1

Answer by ragnaros100 · Jan 17, 2013 at 06:34 PM

You could do it by having a variable, where you store the last selected target. Then once you select a new target, you revert the last targets material to its normal color. Change the newly selected target to Red and assign it then to the variable, that you store the last selected target :)

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

12 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

Related Questions

I want to paint the surface of a mesh depending on if it has been seen by a camera, can this be done with shaders? 0 Answers

Changing two different objects renderer colour 1 Answer

Need help with basic target system 1 Answer

Enable renderer when in line of sight 2 Answers

How to set alpha for object "hidden" by another? 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