- Home /
How to choose between two diffrent idectical objects by clicking?
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;
}
}
}
}
Posting your script will help us see what is going on, and how to suggest you fix/change your code.
Answer by ardizzle · Jul 11, 2013 at 01:54 AM
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;
Answer by youngapprentice · Jul 09, 2013 at 02:29 AM
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 :)
The problem isn't the material. I need the 2 cubes to be identical in every way. But I need it so i can click one while the game is playing and change is color and then i can click the other one and change its color with out disturbing the first cube.
The only problem I have with that is that as the game goes on i don't just plan on having you change color but also being able to move the object, change the size, and even make clones that can be modified separately.
Answer by johnnymoha · Jul 09, 2013 at 03:06 AM
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.
Thats close to what im looking for but I need to actually be able to select the game object its self so I can edit it in other ways like changing its size and position.
I see. You do have a handle to the object's transform: "hit.transform" you can move just that object and scale just that object once you have the handle to it. That's what the ray trace returns for you in the hit.
If your using 3d models/prefabs you want to use what johnnymoha suggested. That is RaycastHit hit and Ray ray. This checks if mouse cursor "touched a model or prefab". the prefabs/models will need a collider component for this to work though.
Then with the "hit" variable you can check which prefab was touched by mouse cursor in a few ways. one way is check the "hit" variable position with prefab positions in the scene to find which prefab was touched by the mouse. From there you know what prefab it was and so make changes to that prefab as you like.
GameObject selectedPrefab;
if (hit.transform.position == myCube.transform.position)
{
//code here to change Prefab myCube
myCube.transform.something here
//or make a reference to the found Cube
selectedPrefab = myCube;
}
If your not using 3dmodels or prefabs then you would want to use the GUI.