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 DibonaJ · Apr 29, 2011 at 01:44 PM · raycastingonguiselectiongui.box

Having to select an object multiple times to continue

Hello everyone. I seem to be having some raycasting issues. With the code I have written I want the user to click on a certain number of objects. After that I want a question to show up that asks them to click on another object. If I have them right click the object the second time through, after the question is posed, it works. But I would rather have them left click. The problem is that if I have it set to have them left click then after they click on the objects the first time, depending on what they click last, the correct or incorrect response shows up without them clicking on the object a second time. My code follows:

void testQuestionThree() { Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition); string finalClick = hit.collider.gameObject.name;

 if(popUpDisplayed == false)
 {
     if ( Input.GetMouseButtonDown(0) )
     {
         if (Physics.Raycast (ray, out hit, 100.0f))
         {
             if(hit.collider.gameObject.name == "learningCylinder2")
             {
                object1Clicked = true;                     
             }

             if(hit.collider.gameObject.name == "learningCube2")
             {
                 object2Clicked = true;
             }

             // below we add a test, to see if we clicked either object,
             // but importantly this only matters if we've clicked both already.
             // this determines what we clicked after the request
             if(object1Clicked && object2Clicked)
             {
                 allObjectsClicked = true;
                 object1Clicked = false; //resetting variables to false
                 object2Clicked = false;
             }
             if((object1Clicked || object2Clicked) && allObjectsClicked)
             {
                 isClicked = true;
                 finalClick = hit.collider.gameObject.name;
                 allObjectsClicked = false;  //resetting variables to false.
                 object1Clicked = false;
                 object2Clicked = false;
             }
         }//end physics
     }//end input.get

     if(allObjectsClicked)
     {
         if(isClicked == false)
         {
             GUI.Box (new Rect (350,10,350,120), "Please click the learning cube.");
         }//end isClicked false
     }

     if(isClicked)
     {
         if(finalClick == "learningCube2")
         {
             GUI.Box(new Rect (350, 10, 350, 120), "That was correct. You may now continue.");
         }

         else
         {
             GUI.Box(new Rect (350, 10, 350, 120), "Sorry, the correct answer was the cube. Click to advance.");
         }                   
         navButtonEnabled = true;
     }//end if isClicked
 }//end popUpDisplayed

}

This is being called in onGUI(). As stated. If I have them right click after the box comes up saying "Please click on learning cube" then it works. But if I them left click then it runs through the whole thing, top to bottom, without the user having to click a second time.

Comment
Add comment
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

1 Reply

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

Answer by Alec-Slayden · Apr 29, 2011 at 01:54 PM

EDIT in response to evolved question / problem:

Looks like my previous answer didn't help prevent multiple OnGUI() calls from running the whole section of script, sorry about that. Here's a solution that will definitely work:

Your testQuestionThree function should look like this:

void testQuestionThree(){

     if(popUpDisplayed == false)
     {
         if(allObjectsClicked)
         {
             if(isClicked == false)
             {
                 GUI.Box (new Rect (350,10,350,120), "Please click the learning cube.");
             }//end isClicked false
         }
         if(isClicked)
         {
             if(finalClick == "learningCube2")
                 {
                 GUI.Box(new Rect (350, 10, 350, 120), "That was correct. You may now continue.");
             }

             else
             {   
                 GUI.Box(new Rect (350, 10, 350, 120), "Sorry, the correct answer was the cube. Click to advance.");
             }                   
             navButtonEnabled = true;
         }//end if isClicked
     }//end popUpDisplayed

}

The upper part has been moved to update in this form:

void Update () {

     if ( Input.GetMouseButtonDown(0) && !popUpDisplayed)
         {
             RaycastHit hit;
             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
             if (Physics.Raycast (ray, out hit, 100.0f))
             {
                 if(hit.collider.gameObject.name == "learningCylinder2")
                 {
                 object1Clicked = true;                     
                 }

                 if(hit.collider.gameObject.name == "learningCube2")
                 {
                     object2Clicked = true;
                 }

                 // below we add a test, to see if we clicked either object,
                 // but importantly this only matters if we've clicked both already.
                 // this determines what we clicked after the request
                 if(object1Clicked && object2Clicked)
                 {
                     allObjectsClicked = true;
                     object1Clicked = false; //resetting variables to false
                     object2Clicked = false;
                 }
                 if((object1Clicked || object2Clicked) && allObjectsClicked)
                 {
                     isClicked = true;
                     finalClick = hit.collider.gameObject.name;
                     allObjectsClicked = false;  //resetting variables to false.
                     object1Clicked = false;
                     object2Clicked = false;
                 }
             }//end physics
         }//end input.get

 }

This ensures that the check for mousedown is only done once per frame, and fixes the issues that were presented by multiple OnGUI Calls. The script itself isn't changed so much as cut into two parts and moved. It's important to keep the OR (||) operand.

finalClick as a variable will need to be declared ahead of time, setting = "" is fine.

Now, I assume you're going to want more test questions in your interactive. Leaving this as is will only work for test question three. If all of your questions involve two objects, then you can use a variable to store the name to test against the clicked object, and not much needs to be changed. If each question has its own dynamic, you might want to turn the stuff we moved to update into a second function, and just have two functions per question; one for the GUI display, and one for the mechanics.

Comment
Add comment · Show 17 · 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 DibonaJ · Apr 29, 2011 at 02:02 PM 0
Share

Using Get$$anonymous$$ouseButton() and Get$$anonymous$$ouseButtonDown() has the same effect. Also, I don't know if I put the right things in Update() or not, but the effect has also been the same.

avatar image Joshua · Apr 29, 2011 at 02:46 PM 0
Share

No it is not. You want to track how often the mouse got clicked. If you use Get$$anonymous$$ouseButton you would get the number of frames it was held down for, if you use Get$$anonymous$$ouseButtonDown you get the number of times it was clicked.

avatar image DibonaJ · Apr 29, 2011 at 02:49 PM 0
Share

Sorry, I didn't mean it had the same effect by definition. I meant it had the same result in my code. No matter which one of those I used it worked out the same.

avatar image Alec-Slayden · Apr 29, 2011 at 03:08 PM 0
Share

I added some detailed info

avatar image DibonaJ · Apr 29, 2011 at 03:29 PM 0
Share

It is still running through the code too quickly. It still doesn't show the GUI box that says "Please click learning cube." If I click on the cube then the cylinder to begin, it says "Sorry, the right answer was the cube." If I click on the cylinder then the cube it says "Correct..." But it still isn't interrupted long enough to display the instructions for the user. I am using Get$$anonymous$$ouseButtonDown and I moved the code you wrote into update, ins$$anonymous$$d of in onGUI.

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

No one has followed this question yet.

Related Questions

Drawing a GUI box by specifying the two corners 1 Answer

Raycasting as a selection tool 1 Answer

GUILayout.Box resizing relative to the containing elements 1 Answer

How to hide GUI.box if condition is false? 1 Answer

How do I make onMouseDown work for OnGUI in C#? 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