Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Nanou · Jan 23, 2015 at 10:01 AM · colliderchildsphereoverlapoverlapsphere

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[i].transform.tag == "Gear")
                     {
                         if (hitColliders[i].transform.parent == connector[0] && hitColliders[i].GetComponent<H_Gear>().connectionset)
                         {
                             connectionset = true;
                             if (!speedset)
                             {
                                 speed -= hitColliders[i].GetComponent<H_Gear>().speed * (hitColliders[i].GetComponent<H_Gear>().size / size);
                                 speedset = true;
                             }
                             connectorC[0] = hitColliders[i].gameObject;
 
                         }
                         else if (connector[1] && hitColliders[i].transform.parent == connector[1] && hitColliders[i].GetComponent<H_Gear>().connectionset)
                         {
                             connectionset = true;
                             if (!speedset)
                             {
                                 speed -= hitColliders[i].GetComponent<H_Gear>().speed * (hitColliders[i].GetComponent<H_Gear>().size / size);
                                 speedset = true;
                             }
                             connectorC[1] = hitColliders[i].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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Owen-Reynolds · Jan 23, 2015 at 03:13 PM

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nanou · Jan 23, 2015 at 03:53 PM 0
Share

$$anonymous$$y code is running it, else the others wouldn't do it either. Plus OverlapSphere always detects its own collider, so nothing to hit can't be possible.

$$anonymous$$y sizes are actually always preset ints, I don't really know why I made that a float..

The thing with the colliders is, every single one of them are triggers, and with everyone, except for #5, it works perfectly. I'll make that problem area, maybe that can lighten things up a bit.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Objects overlapping at origin (0,0,0) (question about OverlapSphere?) 0 Answers

overlap half sphere? 0 Answers

How do i get world position of collider if it inside multiple scaled gameobjects? 0 Answers

Make a simple tree 1 Answer

How is OverlapSphere used? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges