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 mzeman2203 · Jun 24, 2021 at 10:49 PM · collisionrigidbodymeshcollider

Collisions aren't colliding all the time

I'm making a town-building game, and when you want to build a new object you can put it into a new camera view where you get to move around in a third-person view to see where you want to place the new object.alt text

When the object is green you can place it and when it's red you cant. The color-changing code is as followed -

Change Color Script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ChangeColor : MonoBehaviour
 {
     public void CollisionDetected(ChangeColorChild childScript)
     {
     }
 }

ChangeColorChild

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ChangeColorChild : MonoBehaviour
 {
     public Renderer rend;
     public Color originalColor;
 
     void Awake()
     {
         rend = GetComponent<Renderer>();
         originalColor = rend.material.color;
     }
 
     void Start()
     {
         for (int i = 0; i < transform.parent.childCount; i++)
         {
             transform.parent.transform.GetChild(i).gameObject.GetComponent<Renderer>().material.color = Color.green;
         }
     }
 
     void OnDestroy()
     {
        rend.material.color = originalColor;
     }
 
     void OnCollisionEnter(Collision other)
     {
         transform.parent.GetComponent<ChangeColor>().CollisionDetected(this);
 
         if(other.gameObject.name != "Terrain")
         {
             for (int i = 0; i < transform.parent.childCount; i++)
             {
                 transform.parent.transform.GetChild(i).gameObject.GetComponent<Renderer>().material.color = Color.red;
             }
         }
     }
 
     void OnCollisionExit(Collision other)
     {
         transform.parent.GetComponent<ChangeColor>().CollisionDetected(this);
 
         for (int i = 0; i < transform.parent.childCount; i++)
         {
             transform.parent.transform.GetChild(i).gameObject.GetComponent<Renderer>().material.color = Color.green;
         }
     }
 }

alt text

The change color script is on the mine(clone) and the changecolorchild script is on all of the children.

But collisions arent as such, in most cases objects are clearly colliding but the colors are not changing. What do I do?

test5.png (134.3 kB)
test6.png (10.0 kB)
Comment
Add comment · Show 5
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 endasil_unity · Jun 24, 2021 at 11:04 PM 0
Share

The first thing i would do in this case is to add debug logs in the OnCollisionEnter to see if you have cases where collision occurs but color does but change or if the problem is that collision does but occur at all. Narrows down the amount of code you need to troubleshoot in order to understand what is happening.

avatar image mzeman2203 · Jun 24, 2021 at 11:18 PM 0
Share

@endasil_unity my main worry I have is if it has something to do with the lag between the two scripts communicating. Would my code do that?

avatar image endasil_unity mzeman2203 · Jun 24, 2021 at 11:51 PM 0
Share

No, I can't see how lag would prevent it from changing color When two objects overlap. I would probably have tried to solve the problem in a similar way as you except saving the renderers to a list at start instead of going through transform parent children GetComponent etc for every call, but i can't see any reason this would cause this to fail.

If you want to you could export your assets and packages folders and upload somewhere and i can take a look and see if i can see what's wrong in you project.

avatar image mzeman2203 · Jun 25, 2021 at 12:01 AM 0
Share

@endasil_unity I'm okay thanks though, Ill try what you said

avatar image logicandchaos · Jun 25, 2021 at 05:19 PM 0
Share

I find using OnCollisionEnter() and OnCollisionExit() to be unreliable. Think because of multiple triggers, but not sure..

0 Replies

· Add your reply
  • Sort: 

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

217 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity 5 Sphere Rigidbody getting weird behaviour while moving around in a tiled mesh collider scene 1 Answer

Fast moving object not detect collision 3 Answers

Anyway to make my object not fall through the floor without convex? 0 Answers

Why I've a gap between meshcolliders? 0 Answers

Mesh Collider & Rigid Body fail to detect trigger 3 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