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 Domedi · Feb 07, 2014 at 01:07 AM · gameobjectchangeclicking

Want to be able to change color of individual cubes by clicking

I'm making a crude tic tac toe game. Basically what i would like to do is just have a person right click or left click on one of nine cubes and it would change color depending on the click. Problem is when a cube is clicked, all 9 cubes change color. This is the code I'm using, basically taken straight from the first scripting tutorial.

 using UnityEngine;
 using System.Collections;

 public class Cubes : MonoBehaviour
 {
     void Update ()
     {
         if (Input.GetMouseButtonDown (0)) 
         {                                             
             gameObject.renderer.material.color = Color.red;
         }
         
         if (Input.GetMouseButtonDown (1)) 
         {
             gameObject.renderer.material.color = Color.green;
         }                
     }
 }
 

I would like to get this so it updates the block clicked instead of all of them at the same time. I searched the Q&A section here but could not find anything that would fix my issue. Thank you.

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
2
Best Answer

Answer by clunk47 · Feb 07, 2014 at 04:26 AM

All you're doing in your code is checking for mousebutton events. You need to tell the engine where the mouse cursor is, what object you're clicking on. You also probably have this script attached to each cube, that's why ALL of them are changing. Look into Raycast, and use ScreenPointToRay to define your Ray.

Here's an example. Don't forget to either rename the class in the script, or rename the script to match the class name. Attach THIS script to one object only, preferably the main camera.

 using UnityEngine;
 using System.Collections;
 
 public class RaycastExample : MonoBehaviour 
 {
     Ray ray;
     RaycastHit hit;
     
     void Update()
     {
         //Be sure to have a main camera that is tagged "MainCamera" for this example to work.
         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if(Physics.Raycast(ray, out hit))
         {
             //Left Click, change to red.
             if(Input.GetMouseButtonDown(0))
             {
                 hit.collider.renderer.material.color = Color.red;
             }
             
             //Right Click, change to blue.
             if(Input.GetMouseButtonDown(1))
             {
                 hit.collider.renderer.material.color = Color.blue;
             }
         }
     }
 }

You can also use OnMouseOver function for this, which would be a simpler solution but require you to attach the script to every object you'll need to click on. I'd tell you to use OnMouseDown, but OnMouseDown only detects left mouse button. Using OnMouseOver will allow you to detect that you're hovering over the object, then use your Input instruction to determine the button pressed.

 using UnityEngine;
 using System.Collections;
 
 public class OnMouseOverExample : MonoBehaviour 
 {
     void OnMouseOver()
     {
         if(Input.GetMouseButtonDown(0))
             renderer.material.color = Color.red;
         if(Input.GetMouseButtonDown(1))
             renderer.material.color = Color.blue;
     }
 }
 

Comment
Add comment · Show 1 · 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 Domedi · Feb 08, 2014 at 07:53 PM 1
Share

Thank you very much for the help, both solutions worked perfectly. I did have the script attached to each cube. Now on to learn Raycast. Thanks again.

avatar image
0

Answer by Lockstep · Feb 07, 2014 at 01:11 AM

I doubt that you searched thoroughly. The answer to this question can be found easily by doing some tutorials which there are plenty.

Look at this documentation.

Also make sure to read the FAQ and watch the tutorial video before posting a question. I can tell that you didn't since your code is formated poorly.

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

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

Related Questions

Building asset bundles and wanting to move game objects from one parent to a newly created parent 1 Answer

How to change material of gameobject using C# to a material asset. 2 Answers

Any way to change transform.position of an indefinite number of gameobjects on FixedUpdate? 2 Answers

Changing or replace objects 2 Answers

Change Target from Script in GameObject with another Script 1 Answer


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