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 /
  • Help Room /
avatar image
0
Question by AnniJ · Nov 17, 2015 at 02:23 PM · javascriptraycastrendererchange color

Change color of sprite when hit by raycast [SOLVED]

Hello people :)

I'd like to change the color of a sprite in a scene when you look at it.

I checked if theraycast hits anything and it does (the sprites have colliders on them), it all works, but now I that I want to change the color Unity always gives me those "... is not a member of ..." errors :(

Here's the code:

 #pragma strict
 var rend = GetComponent.<Renderer>();
 function Start () {
 
 }
 
 function Update () {
     var hit : RaycastHit;
     var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5,0));
     Debug.DrawRay(transform.position, Vector3(Screen.width*0.5, Screen.height*0.5,0) * 100, Color.green);
     
         if(Physics.Raycast(ray, hit, 100)){
             Debug.Log("Look, there's something!");
             hit.collider.GameObject.rend.material.SetColor ("_Color", Color.red);
             }
 }

Help pls! Thanks :)

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

2 Replies

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

Answer by ZefanS · Nov 19, 2015 at 08:29 AM

Try changing:

 hit.collider.GameObject.rend.material.SetColor ("_Color", Color.red);

to:

 hit.transform.gameObject.GetComponent.<Renderer>().material.color = Color.red;

To have the sprite only change colour while looking at it, you just need to change it back when the ray doesn't hit it. In the snippet below I'm assuming you've tagged all your sprites with the tag "LookSprite". This is an easy way to identify what you've hit with your Raycast. Choose a tag that you like and substitute it for "LookSprite". See here for more on Tags. We can use two scripts to achieve the desired behaviour. The first is attached to all the sprites and makes sure that they change back to their original colour when not hit by the ray.

Colour.js

 #pragma strict
 
 public var change : boolean;
 
 function Start()
 {
     change = false;
 }
 
 function Update()
 {
     if (change == false)
     {
         GetComponent.<Renderer>().material.color = Color.black;
     }
 }
 
 function LateUpdate()
 {
     if (change == true)
     {
         GetComponent.<Renderer>().material.color = Color.red;
         change = false;
     }
 }

The sprite will change colour in the same frame that the boolean "change" is set to true. Now the second script uses a Raycast to set the boolean on the object it hits.

ChangeColour.js

  #pragma strict

  function Update ()
  {
      var hit : RaycastHit;
      var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5,0));
      Debug.DrawRay(transform.position, Vector3(Screen.width*0.5, Screen.height*0.5,0) * 100, Color.green);
      
     if(Physics.Raycast(ray, hit, 100))
     {
         Debug.Log("Look, there's something!");
         if (hit.transform.tag == "LookSprite")
         {
             //You hit a sprite
             hit.transform.gameObject.GetComponent.<Colour>().change = true;
         }
  }
Comment
Add comment · Show 4 · 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 AnniJ · Nov 19, 2015 at 11:29 AM 0
Share

Thank you very much it works now! :D :)

avatar image AnniJ · Nov 24, 2015 at 01:20 PM 0
Share

Thank you very much for the answer :) Sadly it doesn't work :/ I guess because in the else loop ("you hit nothing") it refers to a hit.transform although I'm not hitting anything? I don't know a way around it yet..

avatar image ZefanS AnniJ · Nov 25, 2015 at 12:37 AM 0
Share

You're totally right. I wasn't thinking. Here's an updated and tested solution.

avatar image AnniJ ZefanS · Nov 25, 2015 at 09:28 AM 0
Share

Amazing! You're brilliant! Thanks so much!! Everything works now! :D

avatar image
0

Answer by AnniJ · Nov 19, 2015 at 11:42 AM

So what do I do if I only want the object to have another color while the ray is hitting it?

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 ZefanS · Nov 20, 2015 at 12:44 AM 0
Share

You shouldn't post additional questions as Replies. They are meant to be used to answer the question. Please use the comments for additional questions.

avatar image AnniJ ZefanS · Nov 24, 2015 at 11:12 AM 0
Share

Oh ok sorry I didn't know :(

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

Raycast : Changing hit material back to its original transparency after the ray leaves it. 1 Answer

Storing Renderer in a variable not working! 1 Answer

LineRenderer.SetPosition index out of bounds! Help! 3 Answers

Check if object is visible on the screen and not behind another object 1 Answer

2 same Scripts w/ GetComponent : one works, one doesn't 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