Skip to main content
added 207 characters in body
Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 344

The problem is that when you do a raycast using the mouse position, you are just checking if that raycast hit anything, not if it hit the collider of this particular plant. The collider does not even need to be the collider of a different plant. Any collider you have in your scene will satisfy the condition if (hit.collider != null).

You need to make sure that the collider you are checking against is the one of this particular plant.

How do you get that collider?

When this script is on the same object it controls, then you can do so with GetComponent<Collider>(). But it seems like you've built a controller script which is designed to receives the object it controls in a public property, so the script is not necessarily on the object it controls. So you might have to make the controller of the plant another public field.


But while this should work, you should also be aware that your solution is rather inefficient. It probably works fine with just 3 plants in the scene. But when you have 1000 plants in your scene, then you have 1000 controllers which will all perform the exact same raycast and receive the exact same result which they will then compare against their particular collider. This could cause a notable stutter each time the player clicks. A better solution could be to have a script which only existexists once in the scene which is responsible for handling all mouse clicks and notifyingnotifies the object that was clicked. There is actually a build-in solution for this: The PhysicsRaycaster/PhysicsRaycaster2D component on the camera, an EventSystem component somewhere in the scene (gets created automatically as soon as you add an UI canvas, by the way) and MonoBehaviours implementing the IPointerClickHandler interface.

The problem is that when you do a raycast using the mouse position, you are just checking if that raycast hit anything, not if it hit the collider of this particular plant. The collider does not even need to be the collider of a different plant. Any collider you have in your scene will satisfy the condition if (hit.collider != null).

You need to make sure that the collider you are checking against is the one of this particular plant.

How do you get that collider?

When this script is on the same object it controls, then you can do so with GetComponent<Collider>(). But it seems like you've built a controller script which is designed to receives the object it controls in a public property, so the script is not necessarily on the object it controls. So you might have to make the controller of the plant another public field.


But while this should work, you should also be aware that your solution is rather inefficient. It probably works fine with just 3 plants in the scene. But when you have 1000 plants in your scene, then you have 1000 controllers which will all perform the exact same raycast and receive the exact same result which they will then compare against their particular collider. This could cause a notable stutter each time the player clicks. A better solution could be to have a script which only exist once in the scene which is responsible for handling mouse clicks and notifying the object that was clicked. There is actually a build-in solution for this: The PhysicsRaycaster/PhysicsRaycaster2D component on the camera and MonoBehaviours implementing the IPointerClickHandler interface.

The problem is that when you do a raycast using the mouse position, you are just checking if that raycast hit anything, not if it hit the collider of this particular plant. The collider does not even need to be the collider of a different plant. Any collider you have in your scene will satisfy the condition if (hit.collider != null).

You need to make sure that the collider you are checking against is the one of this particular plant.

How do you get that collider?

When this script is on the same object it controls, then you can do so with GetComponent<Collider>(). But it seems like you've built a controller script which is designed to receives the object it controls in a public property, so the script is not necessarily on the object it controls. So you might have to make the controller of the plant another public field.


But while this should work, you should also be aware that your solution is rather inefficient. It probably works fine with just 3 plants in the scene. But when you have 1000 plants in your scene, then you have 1000 controllers which will all perform the exact same raycast and receive the exact same result which they will then compare against their particular collider. This could cause a notable stutter each time the player clicks. A better solution could be to have a script which only exists once in the scene which is responsible for handling all mouse clicks and notifies the object that was clicked. There is actually a build-in solution for this: The PhysicsRaycaster/PhysicsRaycaster2D component on the camera, an EventSystem component somewhere in the scene (gets created automatically as soon as you add an UI canvas, by the way) and MonoBehaviours implementing the IPointerClickHandler interface.

added 42 characters in body
Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 344

The problem is that when you do a raycast using the mouse position, you are just checking if that raycast hit anything, not if it hit the collider of this particular plant. The collider does not even need to be the collider of a different plant. Any collider you have in your scene will satisfy the condition if (hit.collider != null).

You need to make sure that the collider you are checking against is the one of this particular plant.

How do you get that collider?

When this script is on the same object it controls, then you can do so with GetComponent<Collider>(). But it seems like you've built a controller script which is designed to receives the object it controls in a public property, so the script is not necessarily on the object it controls. So you might have to make the controller of the plant another public field.


But while this should work, you should also be aware that your solution is rather inefficient. It probably works fine with just 3 plants in the scene. But when you have 1000 plants in your scene, then you have 1000 controllers which will all perform the exact same raycast and receive the exact same result which they will then compare against their particular collider. This could cause a notable stutter each time the player clicks. A better solution could be to have one singlea script which only exist once in the scene which is responsible for handling mouse clicks, or to use and notifying the object that was clicked. There is actually a build-in solution of Unity with a PhysicsRaycasterfor this: The PhysicsRaycaster/PhysicsRaycaster2D component on the camera and behavioursMonoBehaviours implementing the IPointerClickHandler interface.

The problem is that when you do a raycast using the mouse position, you are just checking if that raycast hit anything, not if it hit the collider of this particular plant. The collider does not even need to be the collider of a different plant. Any collider you have in your scene will satisfy the condition if (hit.collider != null).

You need to make sure that the collider you are checking against is the one of this particular plant.

How do you get that collider?

When this script is on the same object it controls, then you can do so with GetComponent<Collider>(). But it seems like you've built a controller script which is designed to receives the object it controls in a public property, so the script is not necessarily on the object it controls. So you might have to make the controller of the plant another public field.


But while this should work, you should also be aware that your solution is rather inefficient. It probably works fine with just 3 plants in the scene. But when you have 1000 plants in your scene, then you have 1000 controllers which will all perform the exact same raycast and receive the exact same result which they will then compare against their particular collider. This could cause a notable stutter each time the player clicks. A better solution could be to have one single script responsible for handling mouse clicks, or to use the build-in solution of Unity with a PhysicsRaycaster on the camera and behaviours implementing the IPointerClickHandler interface.

The problem is that when you do a raycast using the mouse position, you are just checking if that raycast hit anything, not if it hit the collider of this particular plant. The collider does not even need to be the collider of a different plant. Any collider you have in your scene will satisfy the condition if (hit.collider != null).

You need to make sure that the collider you are checking against is the one of this particular plant.

How do you get that collider?

When this script is on the same object it controls, then you can do so with GetComponent<Collider>(). But it seems like you've built a controller script which is designed to receives the object it controls in a public property, so the script is not necessarily on the object it controls. So you might have to make the controller of the plant another public field.


But while this should work, you should also be aware that your solution is rather inefficient. It probably works fine with just 3 plants in the scene. But when you have 1000 plants in your scene, then you have 1000 controllers which will all perform the exact same raycast and receive the exact same result which they will then compare against their particular collider. This could cause a notable stutter each time the player clicks. A better solution could be to have a script which only exist once in the scene which is responsible for handling mouse clicks and notifying the object that was clicked. There is actually a build-in solution for this: The PhysicsRaycaster/PhysicsRaycaster2D component on the camera and MonoBehaviours implementing the IPointerClickHandler interface.

added 2 characters in body
Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 344

The problem is that when you do a raycast using the mouse position, you are just checking if that raycast hit anything, not if it hit the collider of this particular plant. The collider does not even need to be the collider of a different plant. Any collider you have in your scene will satisfy the condition if (hit.collider != null).

You need to make sure that the collider you are checking against is the one of this particular plant.

How do you get that collider?

When this script is on the same object it controls, then you can do so with GetComponent<Collider>(). But it seems like you've built a controller script which is designed to receives the object it controls in a public property, so the script is not necessarily on the object it controls. So you might have to make the controller of the plant another public field.


But while this should work, you should also be aware that your solution is rather inefficient. It probably works fine with just 3 plants in the scene. But when you have 1000 plants in your scene, then you have 1000 controllers whowhich will all perform the exact same raycast and receive the exact same result which they will then compare against their particular collider. This could cause a notable stutter each time the player clicks. A better solution could be to have one single script responsible for handling mouse clicks, or to use the build-in solution of Unity with a PhysicsRaycaster on the camera and behaviours implementing the IPointerClickHandler interface.

The problem is that when you do a raycast using the mouse position, you are just checking if that raycast hit anything, not if it hit the collider of this particular plant. The collider does not even need to be the collider of a different plant. Any collider you have in your scene will satisfy the condition if (hit.collider != null).

You need to make sure that the collider you are checking against is the one of this particular plant.

How do you get that collider?

When this script is on the same object it controls, then you can do so with GetComponent<Collider>(). But it seems like you've built a controller script which is designed to receives the object it controls in a public property, so the script is not necessarily on the object it controls. So you might have to make the controller of the plant another public field.


But while this should work, you should also be aware that your solution is rather inefficient. It probably works fine with just 3 plants in the scene. But when you have 1000 plants in your scene, then you have 1000 controllers who will all perform the exact same raycast and receive the exact same result which they will then compare against their particular collider. This could cause a notable stutter each time the player clicks. A better solution could be to have one single script responsible for handling mouse clicks, or to use the build-in solution of Unity with a PhysicsRaycaster on the camera and behaviours implementing the IPointerClickHandler interface.

The problem is that when you do a raycast using the mouse position, you are just checking if that raycast hit anything, not if it hit the collider of this particular plant. The collider does not even need to be the collider of a different plant. Any collider you have in your scene will satisfy the condition if (hit.collider != null).

You need to make sure that the collider you are checking against is the one of this particular plant.

How do you get that collider?

When this script is on the same object it controls, then you can do so with GetComponent<Collider>(). But it seems like you've built a controller script which is designed to receives the object it controls in a public property, so the script is not necessarily on the object it controls. So you might have to make the controller of the plant another public field.


But while this should work, you should also be aware that your solution is rather inefficient. It probably works fine with just 3 plants in the scene. But when you have 1000 plants in your scene, then you have 1000 controllers which will all perform the exact same raycast and receive the exact same result which they will then compare against their particular collider. This could cause a notable stutter each time the player clicks. A better solution could be to have one single script responsible for handling mouse clicks, or to use the build-in solution of Unity with a PhysicsRaycaster on the camera and behaviours implementing the IPointerClickHandler interface.

Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 344
Loading