Script is toggled with a UI button.
If the prefab is on top of the Water element, it should be red/unplaceable. If not, it is green and placeable.
Script 1:
using UnityEngine;
using System.Collections;
public class BuildPlanet : MonoBehaviour {
public GameObject PlaceVis;
public GameObject Water;
Transform PlaceVisMove;
ForTrigger fortrigger;
// Use this for initialization
void Start () {
PlaceVis = (GameObject)Instantiate(Resources.Load("temperateoutpost1transparent"));
PlaceVisMove = PlaceVis.transform;
fortrigger = Water.GetComponent<ForTrigger>();
}
// Update is called once per frame
void Update () {
//re-enables the object if previously disabled
PlaceVis.SetActive(true);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
Vector3 Placement = hit.point;
PlaceVisMove.position = Placement;
PlaceVisMove.up = hit.normal;
//transform up is for rotation
if (!fortrigger.isContact && Input.GetMouseButtonDown(0))
{
Instantiate(Resources.Load("temperateoutpost1"), Placement, PlaceVis.transform.rotation);
PlaceVis.SetActive(false);
enabled = false;
}
if (Input.GetMouseButtonDown(1))
{
PlaceVis.SetActive(false);
enabled = false;
}
if (Input.GetMouseButtonDown(0) && fortrigger.isContact)
{
PlaceVis.SetActive(false);
enabled = false;
}
}
}
}
Script 2 (This script was directly on the Water element):
using UnityEngine;
using System.Collections;
public class ForTrigger : MonoBehaviour {
public bool isContact = false;
public GameObject UserBuilding;
public Material Red;
public Material Green;
// Use this for initialization
void Start () {
//finds the moving prefab, and declares it as variable UserBuilding on start
UserBuilding = GameObject.Find("temperateoutpost1transparent(Clone)");
//UserBuilding is the transparent prefab moved by mouse
Material Green = Resources.Load<Material>("Materials/Green");
Material Red = Resources.Load<Material>("Materials/Red");
UserBuilding.GetComponent<MeshRenderer>().material = Green;
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider col)
{
UserBuilding = GameObject.Find("temperateoutpost1transparent(Clone)");
//if in contact, flip isContact so in BuildPlanet.cs you cannot place the prefab in that location
if (col.gameObject.name == "temperateoutpost1transparent(Clone)")
{
isContact = true;
UserBuilding.GetComponent<MeshRenderer>().material = Red;
}
}
void OnTriggerExit(Collider col)
{
//coroutine instead of standard boolean and rend.material.color because C# doesn't like it for some reason
StartCoroutine("BoolChanger");
}
IEnumerator BoolChanger()
{
UserBuilding = GameObject.Find("temperateoutpost1transparent(Clone)");
yield return new WaitForSeconds(0.0f);
isContact = false;
UserBuilding.GetComponent<MeshRenderer>().material = Green;
}
}
C#. Thanks!