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 JayFitz91 · Jul 30, 2014 at 08:58 PM · raycastmultiply

Raycast hitting more than once

I've just noticed now that my raycast is behaving strangely, when I click on an object I want my ray to get the information of that object and behave accordingly, as of now it seems to multiply that ray each time I click on my objects.

Screenshot when i first click an object: alt text

and heres the second time I click an object: alt text

It has now output as if i've hit the second object twice, and this goes up by one each time i've clicked on a new object. Does anyone know why this is?

Here is my code:

 using UnityEngine;
 using System.Collections;
 
 public class selectObject : MonoBehaviour 
 {
 
     public bool isSelected = false;
 
     public string ballColor = "";
 
     public GameObject selectedBall;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () 
     {
 
         playNextLevel ();
 
         if (Input.GetMouseButtonDown (0))
         {
             RaycastHit hitInfo = new RaycastHit ();
             bool hit = Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
 
             if (Input.GetMouseButtonDown (0) && hit) 
             {
                 Debug.Log("Hit " + hitInfo.transform.gameObject.name);
 
                 if (hitInfo.transform.gameObject.tag == "redBall")
                 {
                     isSelected = true;
                     ballColor = "red";
                     selectedBall = hitInfo.transform.gameObject;
                 }
 
                 else if(hitInfo.transform.gameObject.tag == "blueBall")
                 {
                     isSelected = true;
                     ballColor = "blue";
                     selectedBall = hitInfo.transform.gameObject;
                 }
                 else if(hitInfo.transform.gameObject.tag == "greenBall")
                 {
                     isSelected = true;
                     ballColor = "green";
                     selectedBall = hitInfo.transform.gameObject;
                 }
                 else if(hitInfo.transform.gameObject.tag == "yellowBall")
                 {
                     isSelected = true;
                     ballColor = "yellow";
                     selectedBall = hitInfo.transform.gameObject;
                 }
              }
         }
     }
 }


capture.png (20.4 kB)
capture2.png (34.3 kB)
Comment
Add comment · Show 2
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 robertbu · Jul 30, 2014 at 09:07 PM 0
Share

Any chance you are instantiating an object that has the script above on it? While running and outputting too many log statements, enter the 'selectObject' in the search field in the upper right corner of the Hierarchy window to make sure this script is only on one object.

$$anonymous$$inor nit. You don't have to check On$$anonymous$$ouseDown() again on line 29...just check 'hit'.

avatar image JayFitz91 · Jul 30, 2014 at 09:18 PM 0
Share

That was it exactly @robertu . I had this script on each of my objects, i've taken it off them and placed it on my camera ins$$anonymous$$d which produces the same effect without the result above, thank you

1 Reply

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

Answer by JayFitz91 · Jul 30, 2014 at 09:39 PM

Just making an answer to have it accepted. Thanks to @robertu, i realised i had my script on each of my Instantiated object whereas i should have had it on my camera which only calls one reference to the script

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

22 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2.5D Shooting Rigidbodies vs RayCasts 0 Answers

Most direct way to create OnCursorStay "event" in UI. 1 Answer

Is it possible to allow a raycast to pass through a collider to hit things behind it? 6 Answers

Raycast not detecting anything if MainCamera's X rotation is > 41.5 1 Answer

Follow Nearest Target 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