Is Object At Location?

I have been looking for a while for a solution to my current problem and am wondering if raycasting or colliders would be the best approach.

Here is my scenario - I am currently working on a 3d map editor via raycasting and a gridding system. I am using transparent prefabs that ignore raycast to show the currently selected object to create, then clicking in location creates the object.

I now need to check if there is already an object at this location, if there is, I need to turn the transparent material color red (showing that you can not build here).

Currently I have no code to detect if there is an object currently at this location and not exactly sure where to start. I am working all with C# and really, the event that is triggered doesn’t matter, just a debug statement even saying there is a collision with an object already here would be fine.

I have experimented with colliders with no luck: instantiate and if collider is triggered perform function not working. So decided to wipe the slate clean on this issue and start with some more professional advice on how to tackle it. Thanks.

edit: Code update

I have been attempting with OnCollisionEnter within a script on the object itself.

CODE:

public class EditCollider : MonoBehaviour {
public Color _tmpColor;
public GameObject _tmpObj;
public int _isColliding;

void Start () {
_tmpObj = this.gameObject;
_tmpColor = this.gameObject.renderer.material.color;
}

	void OnCollisionEnter(Collision collision) {
	_isColliding = 1;
	_tmpObj.renderer.material.color = Color.red;
	Debug.Log("Is Colliding: " + _isColliding);
}

void OnCollisionExit(Collision collision) {
	_isColliding = 0;
	if(!_tmpObj)
		return;
    // Unset raycast targets material color
    _tmpObj.renderer.material.color = _tmpColor;
	Debug.Log("Is Colliding: " + _isColliding);
}

}

edit: web build location: http://crystalvortex.ca/Unity/WebPlayer.html

  1. put colliders back on your objects
  2. in your update, check for collisions with existing objects
  3. if collision is detected, change your texture/shader (check out Material) and disallow placement

if your code isn’t working, post it here so we can pick at it :smiley:

Since you’re using a grid system, generally you’d represent the map internally with an array. So you can check if the array location is already occupied.

OK, I have come across a solution, very simple, I had to work around and use a different method, but simple enough, I am using a group of objects with a transparent cube that fills the whole grid space, collider is at .9 scale and constrained all on the rigidbody, then added the basic script:

public class EditCollider : MonoBehaviour {
private Color _tmpColor;
private int _isColliding;

void Start () {
	_tmpColor = this.gameObject.renderer.material.color;
}

void Update() { 
	CheckCollision();
}

void OnCollisionEnter(Collision collision) {
	_isColliding = 1;
}

void OnCollisionExit(Collision collision) {
	_isColliding = 0;
}

public void CheckCollision() { 
	if (_isColliding == 1){
		this.gameObject.renderer.material.color = new Color(255, 0, 0, .5f);
	}
	else { 
		this.gameObject.renderer.material.color = _tmpColor;
		this.gameObject.renderer.material.color = _tmpColor;
		}
}

}

I also updated the Web build to show the results.