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.