Shaders changing colors of both objects after they collide.

Hey all,
I have a “Build” script on two objects used when building. I wanted to change the color of the current object to red if its touching another building. The problem I’m having is that its changing the colors of the current building and the building its touching.

So, to be as specific as possible:
Both game objects have the script, but once the building is placed the Build script is disabled. Here’s the script I’m using, with commented code, to show you what I’ve tried.

using UnityEngine;
using System.Collections;

public class Build : MonoBehaviour {

	private GameObject building;
	private MeshRenderer ren;
	private bool canBuild = true;

	void Start()
	{
		building = gameObject;
	}

	// Update is called once per frame
	void Update () 
	{
		if(Input.GetButtonDown("Fire1") && canBuild)
		{
			gameObject.GetComponentInChildren<BoxCollider>().enabled = true;
			GetComponent<Build>().enabled = false;
			GetComponent<FollowMouse>().enabled = false;
			GameUtilities.MoveToLayer(gameObject.transform, "Building");

		}
		if(Input.GetButtonDown("Fire2"))
			Destroy(gameObject);
	}

	void OnCollisionStay(Collision collision) {
		foreach (ContactPoint contact in collision.contacts) {
			if (contact.otherCollider.tag == "Building")
			{
				Debug.LogWarning(contact.thisCollider.name + " hit " + contact.otherCollider.tag);
				//ren.material.color = Color.red;
				contact.thisCollider.GetComponentInChildren<MeshRenderer>().material.color = Color.red;
				//building.GetComponentInChildren<MeshRenderer>().material.color = Color.red;
				canBuild = false;
			}
			else
			{
				canBuild = true;
				//ren.material.color = Color.white;
			}
		}
	}

	void OnCollisionExit (Collision collision)
	{
		canBuild = true;
		//ren.material.color = Color.white;
	}
}

I’m sure it’s something dumb but I’m really new to unity.

It is changing both objects because disabling a script doesn’t do what most people expect. Callback functions like OnCollisionStay() still continue to function on a disabled script.

http://answers.unity3d.com/questions/657287/what-exactly-does-scriptenabled-false-turn-off.html

So you have a couple of solutions. First, you could just put something like this at the top of the OnCollisionStay() function.

if (!this.enabled) return;

If you are completely done with the script, just delete it rather than disabling it:

Destroy(this);

In your collision code, I believe you are doing far too much work. You don’t have to use the contact list at all. Just test the collision:

if (collision.collider.tag == "Building") {

    // Change the one object.
}