- Home /
TriggerExit not working as intended/Transforming GameObject Problems
I want to help you guys visualize it. When I push a UI button, the Builder script starts. A green transparent cube follows your mouse to show you where it is going to be placed. If it is in contact with the Water (Blue part in photo) the CanPlace bool goes False, so you cannot place it and the material changes to PlaceRed. When it is no longer in contact with the Water object, the bool goes True, and the if statement for placement can be used. Now whenever it goes into contact with the Water element, it goes red, and stays red, but still can be placed when it is off of the Water element. (also the cube is halfway inside the mesh)
Photos of my project which might be needed are here: http://imgur.com/a/RVjm1 (Imgur because I can only have 2 images on the question)
planetstationtransparent.prefab import settings:
planetstation.prefab import settings:
Now it places, but it doesn't place where the transparent cube should be. Builder.cs is what runs when the UI button is pushed and GameObjectDisable is always on.
Script Builder.cs:
using UnityEngine;
using UnityEngine.Events;
using System.Collections;
public class Builder : MonoBehaviour
{
GameObject ghostObject;
Transform ghostObjectTrans;
GameObjectDisable ghostObjectDisable;
// Use this for initialization
void Start()
{
//grabs the can place script so it cannot be placed on water
GameObject Water = GameObject.Find("Water");
// Iwandi : we need to set ghostObjectDisable in the scope of the class to use it in Update
ghostObjectDisable = Water.AddComponent<GameObjectDisable>();
//makes translucent object with red and half opacity TODO: CHANGE BACK TO GREEN
ghostObject = (GameObject)Instantiate(Resources.Load("planetstationtransparent"));
ghostObjectTrans = ghostObject.transform;
}
// Update is called once per frame
void Update()
{
//re-enables the object if previously disabled
ghostObject.SetActive(true);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
Vector3 Placement = hit.point;
ghostObjectTrans.position = Placement;
ghostObjectTrans.up = hit.normal;
//transform up is for rotation
// previous code is for the raycasting of planet mesh
// Iwandi : Each statmen whitin a if() has to resolve to true so writing something == true is like writing (something == true) == true
if (Input.GetMouseButtonDown(0) && ghostObjectDisable.CanPlace)
{
GameObject BuildingFinal = (GameObject)Instantiate(Resources.Load("planetstation"));
Placement = BuildingFinal.transform.position + ghostObjectTrans.up;
enabled = false;
}
if (Input.GetMouseButtonDown(0) && ghostObjectDisable.CanPlace == false)
{
enabled = true;
}
if (Input.GetMouseButtonDown(1))
{
ghostObject.SetActive(false);
enabled = false;
}
}
}
}
Script GameObjectDisable.cs:
using UnityEngine;
using System.Collections;
public class GameObjectDisable : MonoBehaviour
{
[SerializeField]
Material PlaceRed;
[SerializeField]
Material PlaceGreen;
// - we do not use [SerializeField] on canPlace as this is not a value set by the Editor. If you want to debug this value set the Editor/Inspector window to Debug
bool canPlace;
// - Declaring GhostObject within the scope of the class allows all class Methods to access it as a variable
GameObject GhostObject;
// - use a public Property with get;set; to control access to local variables
// - we block external set access to canPlace. this way we know no other script messes with this value
public bool CanPlace { get { return canPlace; } }
// - Your not allowed to Call GameObject.Find() in a c# Constructor so we call it in Start
void Start()
{
GhostObject = GameObject.Find("planetstationtransparent(Clone)");
}
void OnTriggerEnter(Collider col)
{
//if in contact, change to red and make non placeable
if (col.gameObject.name == "planetstationtransparent(Clone)")
{
// - try to avoid Renderer.material as it can memory leak if you don't know what you're doing.
GhostObject.GetComponent<Renderer>().material = PlaceRed;
// - do not redeclare canPlace. We need to set canPlace in the scope of the class to allow accesses later on
canPlace = false;
}
}
void OnTriggerExit(Collider col)
{
//if no longer in contact, change back to green and placeable
if (col.gameObject.name == "planetstationtransparent(Clone)")
{
GhostObject.GetComponent<Renderer>().material = PlaceGreen;
// - same as in CollisionEnter do not
canPlace = true;
}
}
}