- Home /
Click On Objects Within Scene
Hello,
I have no idea how to select objects within my scene that have a collider. I basically want a 2 scripts, one with the clickable command on, and another that makes a variable true if the click is true.
Here is what I have (Big Problem):
I have 3 cubes on my scene, all with the click script and the variable script (within different children). Now when I click on any of the cubes, the variable it true for all the cubes, I only want it true for that cube I just clicked on
(When clicked on cube, a texture is placed around it) - now if I click on one cube, the texture comes on for all the cube. Or sometimes different cubes activate the textures on another cube???
Heres my script:
ClickObject::
#pragma strict
var clicked : boolean = false; var hit : RaycastHit;
static var ClickedOn : boolean = false;
function Update() { if(Input.GetMouseButtonDown(0) && collider.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit, Mathf.Infinity)) { clicked = !clicked; if(tag == "Select") tag = "Unselect"; else tag = "Select"; //Debug.Log("clicked" + (clicked? "" : " off")); }
ClickedOn = clicked;
}
RadiusTexture:
var RadiusObject : Transform; var RadiusSphere : Transform; private var CurrentSize : float;
private var ScaleX = 3.1; //2 private var ScaleY = 0; //0 private var ScaleZ = 3.4; //2.3
private var ScaleXDiv : float = 0; private var ScaleYDiv : float = 0; private var ScaleZDiv : float = 0;
private var ScaleXNew : float = 0; private var ScaleZNew : float = 0;
private var Clicked = false; var MouseOverClicked = false;
var Texture = true;
function Update () {
if(ClickObject.ClickedOn == true)
{
if(Texture == true)
{
CurrentSize = collider.radius;
ScaleXDiv = (ScaleX * CurrentSize);
ScaleXNew = ScaleXDiv / 10;
ScaleZDiv = (ScaleZ * CurrentSize);
ScaleZNew = ScaleZDiv / 10;
RadiusObject.transform.localScale = Vector3(ScaleXNew,ScaleY,ScaleZNew);
}
else
{
CurrentSize = collider.radius;
RadiusSphere.transform.localScale = Vector3(CurrentSize * 3,CurrentSize * 3,CurrentSize * 3);
}
//Clicked = true;
}
else if(ClickObject.ClickedOn == false)
{
RadiusObject.transform.localScale = Vector3(0,0,0);
RadiusSphere.transform.localScale = Vector3(0,0,0);
//Clicked = false;
}
}
What I want is for the cube clicked - to activate the texture, and not interfere with the other cubes/textures
Thanks
Answer by BerggreenDK · Nov 26, 2010 at 04:20 AM
I think you should have a look at this question first. Looks very similar:
http://answers.unity3d.com/questions/28481/clickable-object-problem-help
I believe its all about making the object you can click have the script itself, thats the "object oriented way of doing stuff". You attach the triggerscript to "clickable stuff" instead of a screen/mouse object that scans from point of view.
I've taken a look at that post (which is my older post btw). But its still not answering my question. I need to make the click unique to whatever object I click on. I believe its something to do with the if(ClickObject.ClickedOn == true) in the RadiusScript, how would I make that a unique variable?
let me get this right, you have "some objects" and when one is clicked you need to know exactly which one?
Answer by chris 4.du · Nov 27, 2010 at 10:27 AM
did you duplicate the other two cubes from the original cube? If so, that may the problem.
Your answer
![](https://koobas.hobune.stream/wayback/20220613084556im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can I capture a mouse position on an invisible plane without a collider? 2 Answers
How can I only instantiate if mouse over? 1 Answer
Find out if mouseposition is over gui-element or collider? 2 Answers
OnMouseUp not always firing on mesh that is changing 0 Answers
Touch command for GUIText on Android? OnMouseDown() for Android? 1 Answer