How come certain Children don't cast an Overlapsphere?

Hi there,

I’m making a little game for Android where the player has to connect several gears with eachother by placing new gears on empty screws. Now this is going nicely apart from one little problem.

I’ve got 4 kinds of gears, of which the only difference is their size, which are 3, 4, 5 or 6. When placing a new gear, that gear becomes the child of the screw it is placed on, and checks for connecting gears by casting an OverlapSphere related to their size. Now all of the gears do their job perfectly, except for the one with size 5. As soon as it becomes a child of a screw, it won’t cast the OverlapSphere anymore and I get null references because the code expects it to have at least one overlap. It doesn’t even see its own collider, so I know it just doesn’t cast it at all.

I’ve been testing the OverlapSphere sizes with Gizmos.DrawWireSphere , and even that doesn’t work anymore as soon as it’s a child. It does work when it’s a child of anything but a screw. It needs to be a child since it should check the surrounding screw’s children if theyre connecting.

I have no clue if you need the script for this, since for the other gears it works perfectly, but I’ll post it anyway:

using UnityEngine;
using System.Collections;

public class H_Gear : MonoBehaviour
{

    public Vector3 rot;
    public float size;
    public float speed;
    public bool connectionset;
    public float offset;
    public Y_ButtonManager BM;
    public Transform[] connector = new Transform[2];
    GameObject[] connectorC = new GameObject[2];

    Collider[] hitColliders;

    public bool Gstatic;
    bool speedset;
    bool gotSpeedSource;

    bool gamewonb;
    float gamewon;

    // Use this for initialization
    void Start()
    {
        size = transform.lossyScale.x;
        rot = transform.rotation.eulerAngles;

        BM = GameObject.Find("ButtonManager").GetComponent<Y_ButtonManager>();

    }

    void Update()
    {
        if (gamewonb)
        {
            gamewon += Time.deltaTime;
            if (gamewon > 3)
            {
                Application.LoadLevel(0);
            }
        }

        if (!transform.GetComponent<H_Gear_source>())
        {
            //setting the overlapsphere
            if (size == 6)
            {
                hitColliders = Physics.OverlapSphere(transform.position, 0.85f);
            }
            if (size == 5)
            {
                hitColliders = Physics.OverlapSphere(transform.position, 0.70f);
            }
            if (size == 4)
            {
                hitColliders = Physics.OverlapSphere(transform.position, 0.58f);
            }
            if (size == 3)
            {
                hitColliders = Physics.OverlapSphere(transform.position, 0.43f);
            }
            if (transform.name == "StartGear" || transform.name == "EndGear")
            {
                hitColliders = Physics.OverlapSphere(transform.position, 0.95f);
            }

            //check if any of the ones im overlapping are connected to the source
            if (connector[0])
            {
                int i = 0;
                while (i < hitColliders.Length)
                {
                    if (hitColliders*.transform.tag == "Gear")*

{
if (hitColliders.transform.parent == connector[0] && hitColliders*.GetComponent<H_Gear>().connectionset)
_{
connectionset = true;
if (!speedset)
{_
speed -= hitColliders.GetComponent<H_Gear>().speed * (hitColliders.GetComponent<H_Gear>().size / size);
speedset = true;
_}_
_connectorC[0] = hitColliders.gameObject;*_

}
else if (connector[1] && hitColliders.transform.parent == connector[1] && hitColliders*.GetComponent<H_Gear>().connectionset)
_{
connectionset = true;
if (!speedset)
{_

speed -= hitColliders.GetComponent<H_Gear>().speed * (hitColliders.GetComponent<H_Gear>().size / size);
speedset = true;
_}_
_connectorC[1] = hitColliders.gameObject;
}
}
i++;
}
}*_

//to check if there really is noone connecting me to the source anymore
//yes i know it does the same several times, but combining the statements leads to errors
if ((!connectorC[0] && !connectorC[1] && !transform.GetComponent<H_GearStart>() && !gotSpeedSource))
{
speed = 0;
connectionset = false;
speedset = false;
}
if (connectorC[0] && connectorC[1])
{
if (!connectorC[0].GetComponent<H_Gear>().connectionset && !connectorC[1].GetComponent<H_Gear>().connectionset && !gotSpeedSource)
{
speed = 0;
connectionset = false;
speedset = false;
}

}
if (!connectorC[0] && connectorC[1] && !connectorC[1].GetComponent<H_Gear>().connectionset)
{
speed = 0;
connectionset = false;
speedset = false;
}

if(!connectorC[1] && connectorC[0] && !connectorC[0].GetComponent<H_Gear>().connectionset)
{
speed = 0;
connectionset = false;
speedset = false;
}

}
else
{
//if im connected to the source, i should get my speed from there
speed = transform.GetComponent<H_Gear_source>().speed;
}
}

private void OnDrawGizmos()
{
Gizmos.color = Color.red;
if (size == 6)
{
Gizmos.DrawWireSphere(transform.position, 0.85f);
}
if (size == 5)
{
Gizmos.DrawWireSphere(transform.position, 0.70f);
}
if (size == 4)
{
Gizmos.DrawWireSphere(transform.position, 0.58f);
}
if (size == 3)
{
Gizmos.DrawWireSphere(transform.position, 0.43f);
}

if(transform.name == “StartGear” || transform.name == “EndGear”)
{
Gizmos.DrawWireSphere(transform.position, 0.95f);
}
}

void FixedUpdate()
{
if (transform.name == “EndGear” && connectionset)
{
//gamewonb = true;
}

rot.z += speed;
transform.rotation = Quaternion.Euler(rot.x, rot.y, rot.z);
}

//to get the gears back and place them again
void OnMouseOver()
{
if (Input.GetMouseButtonDown(0))
{
if (!Gstatic)
{
Destroy(gameObject);
if (size == 3)
{
BM.maxCog1++;
}
if (size == 4)
{
BM.maxCog2++;
}
if (size == 5)
{
BM.maxCog3++;
}
if (size == 6)
{
BM.maxCog4++;
}
}
}
}

//This is for connecting gears that are connected to the source and won’t ever disappear
void OnTriggerEnter(Collider other)
{
if(other.GetComponent<H_Gear_source>() && !speedset && other.GetComponent<H_Gear_source>().connectionset)
{
speed -= other.GetComponent<H_Gear_source>().speed * (other.GetComponent<H_Gear_source>().size / size);
speedset = true;
gotSpeedSource = true;
connectionset = true;
}
}

}
I hope anyone can help. If you need pictures of the scene, let me know!

OverlapSphere never just decides to ignore things. Either your code isn’t running it, or there really is nothing for it to hit. Add debug lines.

Maybe your float size isn’t 5 (are you setting size=5, or something funny like size=0.2f*mod1/scaleFactor2;. Debug lines would test that.

Or you messed up a collider (destroyed, set to trigger.) If you want, set up the problem area in a scene by itself, with a 3-line overlapSphere script.