Im trying to write a script so that when you click on a square you can choose it and change its color. Right now I have two squares that are identical in everything but their X and Y position. I also have a red and a blue rectangle that change the color of the sqaures when you click on them. I need to know how to change the color of the selected square instead of both at the same time.
#pragma strict
private var nonSelectedObject : GameObject;
private var selectedObject : GameObject;
function Start ()
{
}
function Update ()
{
EditMode();
}
function EditMode()
{
if(Input.GetMouseButtonDown(0))
{
var hit : RaycastHit;
var ray : Ray = camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray,hit,1000))
{
if(hit.transform // somthing goes here that i can't find == and here )
{
selectedObject = // also not sure what i need to set this equal to.
}
if(hit.transform != GameObject)
{
selectedObject = null;
}
if(hit.transform.name == "Blue")
{
selectedObject.renderer.material.color = Color.blue;
}
if(hit.transform.name == "Red")
{
selectedObject.renderer.material.color = Color.red;
}
}
}
}
You most likely have them sharing the same material!
Also, you don’t need to write a universal script for this! You can add a simple script to each square using OnMouseDown()
When the square senses onMouseDown, change a variable that holds a Transform reference to the squares to that square, and then use OnMouseDown() for the colored rectangles. Run a function that gets the renderer of the currently selected square and set the color
IN Camera:
var hit : RaycastHit;
var ray: Ray;
IN Camera UPDATE:
if (Input.GetMouseButtonDown (0)) { //If Click
clickPos = Input.mousePosition;
ray = camera.ScreenPointToRay (clickPos);
if (Physics.Raycast (ray,hit)) { //IF Click On Object
hit.transform.gameObject.SendMessage("Click");
}
}
On Clickable object:
function Click(){
renderer.material.color = Color.Red;
}
should turn whatever you click on red.
Ok guys thanks a ton for your help. experimenting with your sugenstions led me to what i was looking for. Incase your curious how i solved it or if your looking for the awnswer your self this is the code I wrote :
#pragma strict
static var objectNumber : int; // changes for every object created. Used for makeing each object unique
private var thisObjectsNumber : int; // makes the static variable a non changing private varaible
private var objectName : String; // the name for all objects.
private var selectedObject : GameObject; // var for your selected object. This is the object that gets changed.
private var thisObject : GameObject; // sets the game object to this script
function Start ()
{
// Gives every object its uniqe name when its created.
objectNumber += 1;
thisObjectsNumber = objectNumber;
objectName = "Obj# " + objectNumber;
thisObject = gameObject;
thisObject.name = objectName;
}
function Update ()
{
EditMode();
}
function EditMode()
{
if(Input.GetMouseButtonDown(0))
{
var hit : RaycastHit;
var ray : Ray = camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray,hit,100))
{
// makes sure to not nullify your object when you click off of it
if(hit.transform.tag == "Game Object")
{
if(hit.transform.name == objectName)
{
selectedObject = GameObject.Find(objectName);
}
if(hit.transform.name != objectName)
{
selectedObject = null;
}
}
// keeps you from getting null refrence errors when changing colors with multiple game objects up
if(selectedObject != null)
{
if(hit.transform.name == "Blue")
{
selectedObject.renderer.material.color = Color.blue;
}
if(hit.transform.name == "Red")
{
selectedObject.renderer.material.color = Color.red;
}
}
}
}
}
Just wanted to say thanks again guys;