- Home /
Accessing Colliders in IF statement from PARENT Object.
Hello, This is my Javascript I made, I need to access the collider on these GameObjects to see if the mouse CLICKS on the Mesh of the Object that i have a MESH COLLIDER on.
Code:
var ButtonLeft:Collider;
var ButtonRight:Collider;
var GridCount : float = 0;
function Update () {
GridCount = Mathf.Clamp (GridCount, 0, 3);
}
function OnMouseDownAsButton () {
if (ButtonRight) {
GridCount ++;
}
if (ButtonLeft) {
GridCount --;
}
if (GridCount == 0) {
if (ButtonRight) {
//Move all level objs left one grid
Debug.Log (GridCount);
}
if (ButtonLeft) {
//Move all level objs right one grid
Debug.Log (GridCount);
}
}
if (GridCount == 1) {
if (ButtonRight) {
//Move all level objs left one grid
Debug.Log (GridCount);
}
if (ButtonLeft) {
//Move all level objs right one grid
Debug.Log (GridCount);
}
}
if (GridCount == 2) {
if (ButtonRight) {
//Move all level objs left one grid
Debug.Log (GridCount);
}
if (ButtonLeft) {
//Move all level objs right one grid
Debug.Log (GridCount);
}
}
}
Hierarchy:
In the Above Picture the parent object Arrows has the above script on it.
In the above picture each Child Object has a MESH COLLIDER on it.
Now knowing that where the script is how do I access the colliders IN the IF STATEMENTS, I have
Example:
if (ButtonRight) {
GridCount ++;
}
Thanks for the help! I know its a simple thing, I just cant figure it out! Daniel
$$anonymous$$aybe this is a place where ray casts should come in?
$$anonymous$$
Answer by aldonaletto · Jul 08, 2013 at 01:59 AM
This code is wrong: OnMouseDownAsButton is a custom function, not any valid Unity event, and ButtonLeft and ButtonRight shouldn't be Collider references (using a Collider reference in an if statement only tests whether it's null or not).
If you really want a single script attached to Arrows, you should declare the variables as boolean and use raycast to detect whether one of the arrows was hit:
var ButtonLeft: boolean;
var ButtonRight: boolean;
var GridCount = 0;
function Update(){
// verify if one of the arrows was clicked:
DetectButtonClick();
// now you have ButtonLeft or ButtonRight true if the corresponding
// arrows were clicked - place your code here
}
function DetectButtonClick(){
ButtonLeft = false; // assume no clicks initially
ButtonRight = false;
if (Input.GetMouseButtonDown(0)){ // if left button pressed...
// create a ray passing through the mouse pointer:
var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit: RaycastHit;
if (Physics.Raycast(ray, hit)){ // do a raycast
if (hit.transform.name == "ArrowLeft"){ // left arrow clicked?
ButtonLeft = true;
}
else
if (hit.transform.name == "ArrowRight"){ // right arrow clicked?
ButtonRight = true;
}
}
}
}
EDITED: GetMouseButtonDown was incorrectly written as MouseButtonDown. Answer fixed now.
Wow! Thank you so much, you have been a great help to me! Yes and ill use the booleans to set up the grid system count! :D Thanks! I will Post my updated script with this implemented :D
Btw is I have to modify this to make it work on iphone right? I only need tap. @aldonaletto
@aldoneletto Am I Doing this right? I like the boolean approach, my question is, why doesn't my grid count change when I click? I went through all the logic, and it seems to be correct but I think my problem is maybe the way I increase and decrease the gridcount? Im going to first test if your code works with just a console.log ("you have been hit"); see if that works :D
Thanks again, and for iphone is GetTouches, GetTouch, or Input.touch (0) better?
Code:
var ButtonLeft: boolean;
var ButtonRight: boolean;
var GridCount = 0;
function Update () {
// verify if one of the arrows was clicked:
DetectButtonClick();
// now you have ButtonLeft or ButtonRight true if the corresponding
// arrows were clicked - place your code here
GridSystem ();
}
function DetectButtonClick(){
ButtonLeft = false; // assume no clicks initially
ButtonRight = false;
if (Input.$$anonymous$$ouseButtonDown(0)){ // if left button pressed...
// create a ray passing through the mouse pointer:
var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit: RaycastHit;
if (Physics.Raycast(ray, hit)){ // do a raycast
if (hit.transform.name == "ArrowLeft"){ // left arrow clicked?
ButtonLeft = true;
}
else
if (hit.transform.name == "ArrowRight"){ // right arrow clicked?
ButtonRight = true;
}
}
}
}
function GridSystem () {
GridCount = $$anonymous$$athf.Clamp (GridCount, 0, 3);
if (ButtonRight) {
GridCount ++;
}
if (ButtonLeft) {
GridCount --;
}
if (GridCount == 0) {
if (ButtonRight) {
//$$anonymous$$ove all level objs left one grid
Debug.Log (GridCount);
}
if (ButtonLeft) {
//$$anonymous$$ove all level objs right one grid
Debug.Log (GridCount);
}
}
if (GridCount == 1) {
if (ButtonRight) {
//$$anonymous$$ove all level objs left one grid
Debug.Log (GridCount);
}
if (ButtonLeft) {
//$$anonymous$$ove all level objs right one grid
Debug.Log (GridCount);
}
}
if (GridCount == 2) {
if (ButtonRight) {
//$$anonymous$$ove all level objs left one grid
Debug.Log (GridCount);
}
if (ButtonLeft) {
//$$anonymous$$ove all level objs right one grid
Debug.Log (GridCount);
}
}
}
@aldonaletto I actually am Getting an error. Not sure what it means. Seems like something is $$anonymous$$isspelled maybe..
Sorry to bug you, I found why it was giving an error, It was supposed to be Get$$anonymous$$ouseButtonDown. Right now its throwing a null, (which means the raycast is casting, BUT its not detecting an obj hit).
Here is the console:
You should clamp GridCount after incrementing/decrementing it. Anyway, this code should work - unless the click isn't being detected at all. Add debug lines to know what's happening:
function GridSystem () {
if (ButtonRight) {
GridCount++;
print("Right Arrow clicked");
}
if (ButtonLeft) {
GridCount--;
print("Left Arrow clicked");
}
GridCount = $$anonymous$$athf.Clamp (GridCount, 0, 3);
...
About the iOS version: I suppose that you should modify the DetectButtonClick to use touches ins$$anonymous$$d of mousePosition:
function DetectButtonClick(){
ButtonLeft = false; // assume no clicks initially
ButtonRight = false;
if (Input.touchCount>0){
var touch = GetTouch(0);
if (touch.phase == TouchPhase.Began){ // if screen touch...
// create a ray passing through the touch pos:
var ray: Ray = Camera.main.ScreenPointToRay(touch.position);
var hit: RaycastHit;
if (Physics.Raycast(ray, hit)){ // do a raycast
if (hit.transform.name == "ArrowLeft"){ // left arrow clicked?
ButtonLeft = true;
}
else
if (hit.transform.name == "ArrowRight"){ // right arrow clicked?
ButtonRight = true;
}
}
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613105644im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Help with my code?(NullReferenceException)[CLOSED] 2 Answers
Walking animation problem. 0 Answers
JS Unity Boolean 'not possible to evoke an expression of type 'boolean'' 1 Answer
Unity mathematics phrasing. 1 Answer