Physics.Raycast ignores instantiated objects.
I have objects being spawned on the scene and they perform some actions when hit by Raycast.
If I drag a prefab to the scene manually it works fine, but if objects are respawned(so they are clones), they ignore raycast.
the code which launch those actions is the following:
public void Spin()
{
RaycastHit hit;
Ray ray = new Ray(camera.transform.position, transform.forward);
Debug.DrawRay(camera.transform.position, transform.forward, Color.green);
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.CompareTag("spinner"))
{
hit.collider.gameObject.GetComponent<SpinnerController>().SpinItself();
}
else
{
Debug.Log("not a spinner");
gameObject.GetComponent<AudioSource>().PlayOneShot(loose);
}
}
}
Answer by ppg · Aug 31, 2017 at 09:06 PM
check in inspector what is different between object and object clone during run time/play mode
does the clones have the right tag applied??
for a quick test to find out if its a tagging problem remove the tag check for the ray command
if (hit.collider)
{
hit.collider.gameObject.GetComponent<SpinnerController>().SpinItself();
}
if its a tagging problem you will need to add the missing components or tags during repawn IT IS COMMON TO HAVE SLIGHTLY DIFFERENT SETTINGS ON SPAWNED OBJECTS/clones so changes might be needed in the VOID START/VOID AWAKE/ON SPAWNED()/ON ENABLED etc.. where its appropriate in your case
hope it helps goodluck
if you have time constrains and need a quick fix you might want to assign layers to your ray check to be more precise
Physics.Linecast (transform.position, tfind.currenttarget .transform.position, out hit,1<<8|1<<9);
if you don't know how to use layers effectively here is a good tut on the subject link text