Somehow I have trouble accessing the loop when both NewComponent
and Component
objects are present in the scene. Apologize for this long script.
void FixedUpdate()
{
if(Input.GetMouseButtonDown(0))
{
if(clicked == true)
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit))
{
if(hit.collider.CompareTag("NewComponent"))
{
myOverlapSphereScript=hit.collider.gameObject.GetComponent<OverlapSphereScript>();
foreach (Collider cole in myOverlapSphereScript.objToMove)
{
if(cole.gameObject.CompareTag("NewStarter"))
{
cole.gameObject.tag = "Starterslots";
Debug.Log("slots inside the overlapsphere = "+myOverlapSphereScript.objToMove.Any());
}
}
Debug.Log("Price for this component = RM"+float.Parse(hit.collider.gameObject.GetComponent<Text>().text));
}
if(hit.collider.CompareTag("Component"))
{
Debug.Log(hit.collider.gameObject+" is not a child of "+GameObject.FindGameObjectsWithTag("NewStarter"));
Destroy(hit.collider.gameObject);
cursor.setMouse();
}
}
}
}
}
The problem gets down in below lines of code where both NewComponent
andComponent
is present inside the scene.
foreach (Collider cole in myOverlapSphereScript.objToMove)
{
if(cole.gameObject.CompareTag("NewStarter"))
{
cole.gameObject.tag = "Starterslots";
Debug.Log("slots inside the overlapsphere = "+myOverlapSphereScript.objToMove.Any());
}
}
This lines will only run if only NewComponent
is present in the scene alone but when another Component
is present in the scene, this loop won’t work at other NewComponent
. I’m super confused right now. Any ideas?
UPDATE:
Debugged the code and I found out that the overlapping colliders that overlaps with the NewComponent
will not be detected when both gameobjects are present in the scene, so that’s why the loop won’t run. But still I am not sure why it happen though.