I am beyond confused with this. It tells me that ObjExists isn’t being used with this way of writing the script and it won’t let me place an Object. If I change it so that the bool values get checked after the ray than it doesn’t destroy the instantiated object. This part is essential for my games pathfinding and I can’t find any useful answers that work in this context. Help is greatly appreciated
using UnityEngine;
using System.Collections;
public class Mouse : MonoBehaviour {
RaycastHit hit;
public GameObject Target;
private float raycastLength = 500;
bool ObjExists= true;
void Start () {
bool ObjExists = false;
}
//Next part
//check if object exists already
//if false than create object at mouse location
//if true than destroy instantiate and create new at mouse location
// On Mouse Click Instantiate Target or if it exists Transform location
void Update () {
if (false) {
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, out hit, raycastLength)) {
Debug.Log (hit.collider.name);
Debug.DrawRay (ray.origin, ray.direction * raycastLength, Color.yellow);
if (hit.collider.name == "TerrainMain") {
if (Input.GetMouseButtonDown (0)) {
//Creates Object if it doesn't exist
GameObject TargObj = Instantiate (Target, hit.point, Quaternion.identity) as GameObject;
TargObj.name = "TargetInstantiated";
Debug.Log ("Created");
}
}
}
ObjExists= true;
}
else{
//Destroy object if it exists
if (Input.GetMouseButtonDown (0)) {
Destroy(gameObject.Instantiate);
Debug.Log("DestroyedInstantiate");
}
ObjExists= false;
}
}
}