Check out the attached package. This is a quick attempt to make “fake” shadows in a Unity scene. The theory goes that I’d put colliders in world space which contain a rigidbody and a trigger collider, and once an object hits it, the OnTriggerEnter functions fire and the object entering the trigger changes colour.
It all works, if I attach a rigidbody to the moving object (the cube), but if (as in the attached package) I attach the rigidbody to the world collider, it doesn’t work.
I’m sure I’m having a dumb day or something, but can anyone explain what I’m doing wrong here?
After looking at your code, and having an AHA moment on what you were doing, and after having the thought of… Well, what if the collider isn’t attached to the actual rendering node, which is what your problem actually is.
You need to cycle through the children of the root for all renderers, then change thier material to whatever.
using UnityEngine;
using System.Collections;
public class TheDarkness : MonoBehaviour {
public Material Lite;
public Material Dark;
void OnTriggerEnter ( Collider Coll )
{
Debug.Log ("Entered Trigger");
SetMaterial(Coll, Dark);
//Coll.gameObject.transform.renderer.material = Dark;
}
void OnTriggerExit ( Collider Coll )
{
Debug.Log ("Exited Trigger");
SetMaterial(Coll, Lite);
//Coll.gameObject.transform.renderer.material = Lite;
}
void SetMaterial(Collider Coll, Material mat){
foreach (Renderer rend in Coll.transform.root.gameObject.GetComponentsInChildren<Renderer>()){
rend.material=mat;
}
}
}
Nah, I don’t think that’s it - if you actually run the scene as attached, even the Debug.Log ("blah"); statements don’t get called. Basically, the triggers aren’t being triggered…!
Actually, I don’t think that is right. Having thought about it, what I think is happening is that the rigidbody attached to the static object is actually going to sleep before the object which is just a collider is hitting it. This acts as though there is no rigidbody attached.
The whole purpose of this exercise is to keep the moving objects working without rigidbodies, so having to keep a rigidbody on the object in question would pretty much be a fail from that perspective. It’s such a shame that you can’t actually detect collisions between objects without having at least one rigidbody…