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 riontamer · Dec 01, 2012 at 12:23 AM · prefabinputmouse input

Specific Items Being Clicked

Relative newbie, and I have a few questions, and one that's on the top of my head involves mouse inputs. I have a really simple scene, testing out a shuffle bag script I found on the forums here. In the scene there's a flat plane, a cube, and a sphere. I have this script attached to the cube…

 #pragma strict
 #pragma implicit
 
 static class ShuffleBag{ 
 
     var data = new Array(); 
     var cursor=-1;
     
     function add(item, num : int) {
         var i = num || 1;
         while(i--)
         {
             data.push(item);    
         }
         cursor = data.length -1;
     } 
     
     function next() { 
         var grab; 
         var temp;
         if(cursor<1) { 
             cursor = data.length-1; 
             return data[0]; 
         }
         grab = Random.Range(0, cursor+1);
         temp = data[grab];
         data[grab]= data[cursor];
         data[cursor] = temp;
         cursor--;
         return temp;
     }
     
 }
      
 function Start()
 {
 
 }
 
 var display : GUIText;
 var displaylife = 1.0;
 
 function Update()
 {
     
      if(Input.GetMouseButtonDown(0)){
         var hit : RaycastHit;
         var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if (Physics.Raycast(ray, hit)){
             //startTime = Time.time;
             //point = hit.point;
             //object = hit.transform.gameObject;
             
             ShuffleBag.add(0,1);
             ShuffleBag.add(1,3);
             print(ShuffleBag.next());
             
             if(ShuffleBag.next() == 1){
                 var clonedisplay = Instantiate(display);
                 display.text = 'Hit!';
                 Destroy(clonedisplay, displaylife);
             }
             
             else if(ShuffleBag.next() == 0){
                 var clondedisplay = Instantiate(display);
                 display.text = 'Miss!';
                 Destroy(clonedisplay, displaylife);
             }
         }
     }
     else if (Input.GetMouseButtonUp(0)){
         //duration = Time.time - startTime;
         Destroy(clonedisplay, displaylife);
     }
     
     Destroy(clonedisplay, displaylife);
 
 }

With a floating camera and when I point my camera at the cube, sphere, or plane, I get the same results from the if statements nested in Update(). How do I make it so it only the cube creates the GUItext display?

I know it's bad manner to ask two questions in one, but additionally, I'm curious why when the prefab for GUIText comes up, eventually the Hit! display won't be destroyed / removed and gets stuck on screen. Any idea how to better employ this display?

Thanks in advance!

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 riontamer · Dec 01, 2012 at 06:09 PM 0
Share

$$anonymous$$y bad, just realized I have 'var' in front of the clonedisplay Instantiations, should remove those. Still that doesn't change the click complication…

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by riontamer · Dec 01, 2012 at 07:14 PM

solved my own question, combining things…

 function Update(){
     var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     var hit : RaycastHit;
     if (collider.Raycast (ray, hit, 100.0)) {
         if (Input.GetMouseButtonDown(0)){
             ShuffleBag.add(0,1);
             ShuffleBag.add(1,3);
             RES = ShuffleBag.next();
             
             print(ShuffleBag.next());
             
             
             if(RES == 1){
                 clonedisplay = Instantiate(display);
                 clonedisplay.guiText.text = 'Hit!';
                 Destroy(clonedisplay, displaylife);
             }
             
             else if(RES == 0){
                 clonedisplay = Instantiate(display);
                 clonedisplay.guiText.text = 'Miss!';
                 Destroy(clonedisplay, displaylife);
             }
         }
     }
 }

The script checks to see if the POV of the character is pointing at the object, distance being 100 in this circumstance, but can be lengthened or decreased. THEN, instead of before, the if statement for Input.GetMouseButtonDown(0) comes into play. So, is the player looking at the object THEN is the mouse being clicked. Also fixed the display by having the global variables changed to…

 var display : GameObject;
 display.AddComponent("GUIText");
 var displaylife = 1.0;

Essentially treating the text as a gameobject that has GUIText as a component rather than a GUIText object itself.

Finally (and even more unrelated to the topic, but for revisions sake) changed Shufflebag.next() to a global variable called RES so the outcome doesn't differ between the two if statements that are checking it. Kind of stupid on my part not to have had that before.

Hope this helps someone caught in the same spot of trouble as I was.

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

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

11 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

Related Questions

trouble with instantiate 1 Answer

Player Input Action Map is being deleted when I import prefab to the scene 0 Answers

First Person Controller Erratic 1 Answer

Prefab Puzzle 1 Answer

Restrict touch input 0 Answers


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