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 LazyBassTurd · Jul 09, 2013 at 10:26 AM · javascriptmousegridselection

Tile based selection deselection

Hi, I'm very new to unity and javascript and trying to create a grid based tile system. Please be gentle :)

I have created a 10x10 grid of tiles and attached the follow code to each. I can highlight the tile (red) when the mouse cursor is over it and select the tile (green) when clicked on, however I would like to deselect any other tiles selected before selecting a new one to have just one tile selected at a time. Hope that makes sense :)

 var origTileColor : Color;
 var tileSelected = false;
 
 function OnMouseOver () {
     if(Input.GetMouseButtonDown(0)) {
         tileSelected = true;
         Debug.Log(transform.position);
     }
     
     if(tileSelected) {
         renderer.material.color = Color.green;
     }else{
         renderer.material.color = Color.red;
     }
 }
 
 function OnMouseExit() {
     if(!tileSelected) {
         renderer.material.color = origTileColor;
     }
 }



======================================================================

Thanks for the tips guys, I decided to rethink my code and found something that works however, I'm having some problems implementing a hover color change when the mouse is over a tile. Any help would be great.

 var myTiles : GameObject[];
 
 myTiles = GameObject.FindGameObjectsWithTag("Tile");
 
  
 function Update () {
 
     if (Input.GetButtonDown ("Fire1")) {
     
          var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
          
         var select : RaycastHit;
  
         if (Physics.Raycast (ray, select, 100.0)){
         
             for (var thisTile in myTiles){
             
                 if(select.collider.gameObject == thisTile){
                 
                     thisTile.renderer.material.color = Color.green;
                     Debug.Log(thisTile);
                     
                 }else{
                 
                     thisTile.renderer.material.color = Color.white;
                 
                 }
             }
         }
     }
 }
Comment
Add comment · Show 5
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 prototype7 · Jul 09, 2013 at 11:22 AM 0
Share
 function On$$anonymous$$ouseOver()
     {
         
         if (Input.Get$$anonymous$$ouseButtonDown(0))
         {
             if (tileSelected) tileSelected = false;
             if(!tileSelected) tileSelected = true;
             Debug.Log(transform.position);
         }
 
         if (tileSelected)
         {
             renderer.material.color = Color.green;
         }
         else
         {
             renderer.material.color = Color.red;
         }
     }

// Edit this won't work, sorry i didn't test it, my suggestions is to use list

 var myList : List.<Type> = new List.<Type>(); 

or use 2D Array

avatar image LazyBassTurd · Jul 09, 2013 at 11:33 AM 0
Share

Thanks for your time but, maybe I wasn't clear enough. If I select a tile I would like all other tiles to deselect first, only having one tile selected at any given time.

avatar image prototype7 · Jul 09, 2013 at 11:45 AM 0
Share

If you're using array or list, you can use loop to find tileSelected

avatar image swyrazik · Jul 09, 2013 at 11:48 AM 0
Share

I would prefer to use a Tile$$anonymous$$anager class that will hold in a variable the selected tile, which will be able to easily deselect when selecting another tile.

avatar image LazyBassTurd · Jul 09, 2013 at 02:13 PM 0
Share

I have updated my question with new code, need some help with hover effects if you have the time.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by LazyBassTurd · Jul 09, 2013 at 03:47 PM

Figured this out, for anyone else wanting the same functionality:

GameBoard Script

 var myTiles : GameObject[];
 myTiles = GameObject.FindGameObjectsWithTag("Tile");
  
  
 function Update() {
  
     if(Input.GetButtonDown ("Fire1")) {
     
         var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition); 
         var select : RaycastHit;
  
         if(Physics.Raycast (ray, select, 100.0)){
  
             for(var thisTile in myTiles){
  
                 if(select.collider.gameObject == thisTile){
      
                     thisTile.renderer.material.color = Color.green;
      
                 }else{
      
                     thisTile.renderer.material.color = Color.white;
                          
                 }
             }
         }
     }
 }

GameTile Script

 function OnMouseOver() {
 
     if(renderer.material.color == Color.white) {
         renderer.material.color = Color.red;
     }
 }
 
 function OnMouseExit() {
 
     if(renderer.material.color == Color.red) {
         renderer.material.color = Color.white;
     }
 }

 
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

What is wrong with my code? 1 Answer

Mouse position to Isometric Grid Tile number 1 Answer

Bullets follow mouse after shooting in Sidescroller 1 Answer

Weapon selection system 2 Answers

Detecting Mouse Scroll Wheel? 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