- Home /
Physics.RayCast problem with instantiated prefab
I have a piece of code that relies on a Physics.Raycast to select an object which is an instantiated prefab. This code works in Unity version 2017.3.1f1 (windows10) but in subsequent versions of Unity it fails. This is whilst running the scene in the editor.
To isolate the problem I created a test scene and directly placed the prefab into the scene with the editor and placed a second via instantiation whilst running the scene. When I run this test code in Unity 2017.3.1f1 both objects can be clicked on successfully. Whilst running under Unity 2018.2.0f2 only the prefab added via the editor is selectable, the one added by instantiation is not selectable.
Has there been some recent change with Physics.RayCast / Prefabs / Object Instantiate that could lead to this behavior?
Any answers / suggestions would be much appreciated.
I'm using TouchScript to handle taps in my code, with tap being handled as follows.
private void tapHandler(object sender, EventArgs e)
{
Debug.Log ("CameraController : tapHandler ");
var gesture = sender as TapGesture;
TouchHit hit;
gesture.GetTargetHitResult(out hit);
Debug.Log ("CameraController : TAPPED Object " + hit.Transform.name+" Point "+hit.Point);
Vector2 vec = gesture.NormalizedScreenPosition;
Vector2 vec2 = gesture.ScreenPosition;
Debug.Log ("CameraController : Normalized " + vec + " Screen Pos " + vec2);
Ray ray = Camera.main.ScreenPointToRay (new Vector3 (vec2.x, vec2.y, 0));
RaycastHit rHit;
Debug.DrawRay (ray.origin, ray.direction * 200, Color.red, 10.0f);
bool found = Physics.Raycast (ray, out rHit, 200.0f);
if (found) {
Debug.Log ("Hit something "+ rHit.transform.name);
showSelected = true;
Text text = GameObject.Find ("Location").GetComponent<Text> ();
text.text = rHit.transform.name;
}
}
I'm having a similar issue using Physics2D.RaycastAll and Unity 2018.2.3.f1 - the RaycastAll call finds objects manually placed in the scene, but not those instantiated at runtime. Have you found any solutions/workarounds for this?
Your answer
Follow this Question
Related Questions
Raycasting 1 Answer
Prefabs dont instantiate directly on point 0 Answers
Referencing gameObject from script after Instantiate 0 Answers
Prefab of a whole scene? 3 Answers