- Home /
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.
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;
}
}
}
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?
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.
@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?
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.
@endasil_unity I'm okay thanks though, Ill try what you said
I find using OnCollisionEnter() and OnCollisionExit() to be unreliable. Think because of multiple triggers, but not sure..