Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Daniel G · Jul 08, 2013 at 12:12 AM · javascriptcollidersparent and childaccessing scripts

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:

alt text

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

screen shot 2013-07-07 at 8.08.59 pm.png (5.5 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Daniel G · Jul 08, 2013 at 12:33 AM 0
Share

$$anonymous$$aybe this is a place where ray casts should come in?

$$anonymous$$

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Daniel G · Jul 08, 2013 at 02:26 AM 0
Share

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

avatar image Daniel G · Jul 08, 2013 at 02:45 AM 0
Share

@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);
         }
     }
 
 }
 
avatar image Daniel G · Jul 08, 2013 at 02:51 AM 0
Share

@aldonaletto I actually am Getting an error. Not sure what it means. Seems like something is $$anonymous$$isspelled maybe..

alt text

screen shot 2013-07-07 at 10.50.14 pm.png (37.6 kB)
avatar image Daniel G · Jul 08, 2013 at 03:01 AM 0
Share

@aldonaletto

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:

alt text

screen shot 2013-07-07 at 11.00.00 pm.png (31.6 kB)
avatar image aldonaletto · Jul 08, 2013 at 03:28 AM 0
Share

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;
         }
       }
     }
   }
 }
Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges