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
0
Question by ratmstein · Sep 08, 2013 at 06:32 PM · materialcoloronmousedown

How to change sphere color when clicked?

I am trying to change the color of a sphere when I click on it. I am using this code

 void OnMouseDown ()
     {
     
         rigidbody.useGravity = true;
         renderer.material.color = Color.black;
         }
 }

This is in a script that I have given to my sphere. I am thinking my problem may be the sphere not knowing it has been clicked or something?

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 aldonaletto · Sep 08, 2013 at 06:41 PM

This should work - provided that the sphere has a collider, isn't hidden by another collider and isn't in the IgnoreRaycast layer. You should also make sure that the script is attached to the sphere (not to a child or parent), and that the shader doesn't ignore the material.color parameter. There's also an extra "}" in your code, but I suppose that it's a typo in the question. NOTE: OnMouseDown isn't emulated by touches - only a mouse or similar pointing device generates this event.

Comment
Add comment · Show 9 · 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 ratmstein · Sep 08, 2013 at 06:51 PM 0
Share

The sphere has a collider. I don't know anything about the IgnoreRaycast layer, and the shader is on diffuse. Still no luck making the color change though. Also that last "}" is a typo.

avatar image aldonaletto · Sep 08, 2013 at 07:00 PM 0
Share

The diffuse shader is ok. I edited my answer and included other topics: is any collider hiding the sphere? (a transparent plane, for instance). Is this being tested in a touchscreen device? (On$$anonymous$$ouseDown don't work with touches). I tested your code and it worked fine with a Unity's sphere, as expected. Try to add a debug line:

 void On$$anonymous$$ouseDown (){
     print(name + " clicked");
     rigidbody.useGravity = true;
     renderer.material.color = Color.black;
 }
avatar image ratmstein · Sep 08, 2013 at 08:13 PM 0
Share

I don't see any difference after adding the debug line. I see where it says IgnoreRaycast under layers and that is not checked.

avatar image DDP · Sep 08, 2013 at 08:19 PM 0
Share

Do you see something being written in the debug window when you click on the sphere? The debug window can be found in the Window -> Console menu.

It should say something like: "Sphere clicked". If it doesn't show, it means that the application is not able to detect the click itself, so the issue is not likely to reside in the code.

In the Edit -> Project Settings -> Physics menu, are all boxes selected? If not, try selecting all of them.

avatar image ratmstein · Sep 08, 2013 at 08:39 PM 0
Share

I'm not using a touchscreen device. Where are the results from the debug line supposed to print? I don't see anything different.

Show more comments
avatar image
0

Answer by DDP · Sep 08, 2013 at 06:41 PM

You will want to add a SphereCollider component to your game object.

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

17 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Material doesn't have a color property '_Color' 4 Answers

Changing two different objects renderer colour 1 Answer

Color wrong on IOS device 1 Answer

Instantiating objects with their own materials 0 Answers

How to access Emission Color of a Material in Script? 5 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