- Home /
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;
}
}
}
}
}
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
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.
If you're using array or list, you can use loop to find tileSelected
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.
I have updated my question with new code, need some help with hover effects if you have the time.
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;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613105745im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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