- Home /
How i do a clicker with moving objects?
Hi! My problem is i do right now clicker where objects moving and when you click it you get a point. I tried 2 difrent code but they don't work. Help me please. Here the code:
 public class click : MonoBehaviour//first
 {
     public int score;
 
      void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
 
             RaycastHit hit;
             if (Physics.Raycast(ray, out hit))
             {
                 score++;
             }
         }
     }
 
 }
2
 public class click : MonoBehaviour//second
{
     public int score;
 
      void onClick()//event trigger is using
 {
 score++;
 }
}
When you click the object you don’t get the poin.
And what are the results of your investigations?
- Do you get any runtime error in the console? 
- Where have you attached this script? 
- Do the objects you want to click have colliders? A raycast can't detect objects without colliders... 
- Is the - Updatefunction called?
- Is the - if (Input.GetMouseButtonDown(0))evaluated to true?
- Have you debugged the ray to see where it's going? 
- Is the raycast hitting something? If so which object? 
- Is the score incrementing in the inspector? 
I should not have to ask all those questions... This is ssome investigations you should have done yourself before asking for help.
Thank you for showing me that i am not alone with this feeling..
Answer by Hazsha · Dec 08, 2021 at 10:29 AM
First script is correct!!! 1. you need to attack collider to moving game object 2. Don't use RaycastHit inside update. just make them as variable outside of the Update();
Your answer
 
 
             Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Quad Objects not responding to clicks. 1 Answer
Multiple Cars not working 1 Answer
moving platforms? 0 Answers
Cliks per second does not work 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                