I have the following task:
I have some cubes, that the player can pick up and move to a slot (a simple plane). The moment the player moves over the slot, the planes should change its color to indicate that it can now be snapped into the slot. That works so far. But if the user decides to move the cube away from the slot, I have to assign the original color to that slot. For that I save the slot in a variable “currentSlot”. I reassign the old color (doesn’t work) and then I need to erase the currentSlot variable. But I do not know how to do that. As a workaround I assigned it a empty dummy object. But that’s not a solution I’m satisfied with. Has anyone a better idea?
Here is my code so far:
function GetSlot() //: GameObject
{
var hit : RaycastHit; //Saves the hitted object
var ray = new Ray(transform.position, (-transform.up)); //check object under the cube
if (Physics.Raycast (ray, hit, 10.0)){
//Debug.Log(hit.collider.gameObject.tag);
if(hit.collider.gameObject.tag =="slot")
{
currentSlot = hit.collider.gameObject;
originalSlotColor = currentSlot.renderer.material.color;
currentSlot.renderer.material.color = activeSlotColor;
}
else
{
currentSlot.renderer.material.color = originalSlotColor;
currentSlot = GameObject.Find("DummyObject");
}
}
Debug.Log(currentSlot);
}