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 /
This question was closed Mar 20, 2015 at 06:35 AM by galaboy for the following reason:

Other

avatar image
0
Question by galaboy · Mar 19, 2015 at 12:12 PM · raycastscriptingbasicsmouseclick

need help on raycast

hi, i have a script which has to detect the gameobject from screen which the mouse clicks and display the name of it. am trying to figure out where am making the mistake. need help on this.

 using UnityEngine;
 using System.Collections;
 
 public class eventManager_2 : MonoBehaviour {
 
     public GameObject cardPrefab;
 
     // Use this for initialization
     void Start () {
 
         cardCreation ();
     }
     
     // Update is called once per frame
     void Update () {
 
         if (Input.GetMouseButtonDown(0)) 
         {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
             if(Physics.Raycast(ray,out hit))
             {
                 print (hit.collider.name);
                 
             }
         }
     }
     
     void cardCreation()
     {
         float heightOffset = (cardPrefab.transform.position.y) + 1.2f;
 
         for(int i = 1; i < 10; i++)
         {
             //for(int j = i; j > i; j++)
             //{
             GameObject cardsTemp =  (GameObject)Instantiate(cardPrefab);
             //cardsTemp.transform.position = new Vector3(-8.0f ,(transform.position.y - 3.0f + i),0);
             cardsTemp.transform.position = new Vector3(-8.0f , (i * heightOffset) - 4.8f ,0);
             //}
         }
     }
 }
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 SingularDeviation · Mar 19, 2015 at 02:00 PM 0
Share

Could you give some more information on what's going wrong? Is it not working at all, or is it just buggy?

avatar image Utamaru · Mar 19, 2015 at 03:07 PM 1
Share

Do you have right colliders on your gameObjects? For example it must have BoxCollider, but not BoxCollider2D.

1 Reply

  • Sort: 
avatar image
1

Answer by Utamaru · Mar 19, 2015 at 03:14 PM

I would recommend you to use new unity EventSystems. For example, if you want to catch clicks on your gameObjects, just implement IPointerClickHandler interface in script, attached to your gameObject. And then, when click on your object happens, OnPointerClick message will be called. You also need to add PhysicsRaycaster component to your main camera. Don't forget to do that.

Example code from my project:

 public class FindableObject : MonoBehaviour, IPointerClickHandler
 {
     public void OnPointerClick (PointerEventData eventData)
     {
         Debug.Log ("OnPointerClick " + gameObject.name);
     }
 }

Comment
Add comment · Show 1 · 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 galaboy · Mar 20, 2015 at 06:12 AM 0
Share

actually its my fault, i didn't scale the collider according to the size of the prefab. the script is fine its working.

thanks Utamaru for showing ur response.

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

Mouse controlled Laserpointer 1 Answer

Mouse clicking on an object (Raycast.hit) for another object to connect to it? 0 Answers

in rewarded ad which code means user closed the ad by itself 2 Answers

Mouse button not detected with "Fire1" and Colliders 2 Answers

After upgrade to unity 2019.3, my mouse/ click drag object placement is all over the place 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