Question by
CybernetHacker14 · Jul 25, 2018 at 02:49 PM ·
scripting problembug-perhaps
Need help with a script execution fault
I have a script execution problem. Basically in the following attached code, the script works perfectly at the starting first usage iteration. But then when I repeat the process the second time, the value of _isSelecting variable is not updated, as I see it in the Inspector Window. I need to see if the fault is in my scripting, or is there a problem in the compiler.
using UnityEngine.EventSystems;
using UnityEngine;
using System.Collections.Generic;
public class EngineClickHandlerModule : MonoBehaviour {
// Access different modules of Engine
EngineSelectionModule _sm;
EngineUnitFormationModule _ufm;
EngineBuildingPlacementModule _blpm;
[SerializeField]
List<GameObject> selectedObjects = new List<GameObject>();
[SerializeField]
bool _isSelecting = false;
Vector3 _mousePosition1;
Vector3 _mousePosition2;
// Use this for initialization
private void Awake()
{
_sm = GameObject.Find("SelectionManager").GetComponent<EngineSelectionModule>();
_ufm = GameObject.Find("UnitFormationManager").GetComponent<EngineUnitFormationModule>();
}
// Update is called once per frame
void Update () {
if (EventSystem.current.IsPointerOverGameObject())
{
_isSelecting = false;
return;
}
if (Input.GetMouseButton(0))
{
if (_isSelecting == false)
{
Debug.Log("Clicking");
foreach(var selectableObject in FindObjectsOfType<UnitSystems>())
{
if (!IsWithinSelectionBounds(selectableObject.gameObject))
{
selectableObject.transform.root.Find("SelectionCircleHalo").gameObject.SetActive(false);
}
}
selectedObjects.Clear();
_sm.ResetUnitSelection();
_mousePosition1 = Input.mousePosition;
_isSelecting = true;
}
}
else
{
_isSelecting = false;
}
if (_isSelecting)
{
foreach(var selectableObject in FindObjectsOfType<UnitSystems>())
{
if (IsWithinSelectionBounds(selectableObject.transform.root.gameObject))
{
if (!selectedObjects.Contains(selectableObject.transform.root.gameObject))
{
selectableObject.transform.root.Find("SelectionCircleHalo").gameObject.SetActive(true);
selectedObjects.Add(selectableObject.transform.root.gameObject);
}
}
}
}
if (Input.GetMouseButtonUp(0))
{
_mousePosition2 = Input.mousePosition;
if(_mousePosition1 == _mousePosition2)
{
ClickOperations(0);
}
else
{
_sm.SetMultipleSelectedUnitsList(selectedObjects);
}
}
if (Input.GetMouseButtonDown(1))
{
_isSelecting = false;
if (_sm._areMultipleUnitsSelected)
{
ClickOperations(3);
}
else
{
ClickOperations(1);
}
}
}
void ClickOperations(int caseNo)
{
RaycastHit hitInfo = new RaycastHit();
bool _hit = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
if (_hit)
{
switch (caseNo)
{
case 0:
_sm.CheckOnLeftSingleClickConditions(hitInfo);
break;
case 1:
_sm.CheckOnRightSingleClickConditions(hitInfo);
break;
case 3:
_sm.CheckOnRightMultipleClickConditions(hitInfo);
break;
}
}
}
public List<GameObject> GetSelectedUnits()
{
return selectedObjects;
}
public bool IsWithinSelectionBounds(GameObject gameObject)
{
if (!_isSelecting)
return false;
var camera = Camera.main;
var viewportBounds = Utils.GetViewportBounds(camera, _mousePosition1, Input.mousePosition);
return viewportBounds.Contains(camera.WorldToViewportPoint(gameObject.transform.position));
}
void OnGUI()
{
if (_isSelecting)
{
var rect = Utils.GetScreenRect(_mousePosition1, Input.mousePosition);
Utils.DrawScreenRect(rect, new Color(0.8f, 0.8f, 0.95f, 0.25f));
Utils.DrawScreenRectBorder(rect, 2, new Color(0.8f, 0.8f, 0.95f));
}
}
}
Comment