Tag: mouse function

Selection Script for Unity3D

This script is royalty free; you can use it for whatever you want to. What it does is when you click an object, the object is highlighted. When you click on it again, the object is un-highlighted, back to normal.

Just copy and paste this script into your JavaScript in Unity. Then attach the script to any visible object in your scene, including 3d text.

Here it is:

// Declare our variables...

var selectedColor : Color = Color(0.2, 0.3, 0.4); //This will give us a color wheel to choose a color from.
private var isSelected : boolean = false; // Simple boolean variable. Can be either true or false.

// Okay, now for the functions.
// The function OnMouseDown means to only do whats in the function when the user clicks the mouse.

function OnMouseDown ()
{
//isSelected = isNotSelected...
isSelected = !isSelected;

// If it's unselected, make sure the material color is white.
if (!isSelected) {
renderer.material.color = Color.white;

}

// If it's selected, change the material color to the selectedColor.
if (isSelected) {
renderer.material.color = selectedColor;

}

}

// Feel free to use this script in commercial or non-commercial purposes.
// My name in the credits somewhere would be appreciated, though.

Advertisements