// Update is called once per frame
    void Update()
    {
        if (modeManager.IsSimulate)
        {
            GameObject empty = (GameObject)GameObject.Find("New Game Object");
            if (empty)
            {
                GameObject.Destroy(empty);
            }
        }
        else if (modeManager.IsAddModule)
        {
            PlotAvailableNodes(true);
            nodeUnderMouse   = FindNodeUnderMouse();
            moduleUnderMouse = FindModuleUnderMouse();

            if (nodeUnderMouse != null)
            {
                FindAvailableSpots();
                PlotAvailableSpots();
            }
            if (moduleUnderMouse != null)
            {
                Renderer rend = ((GameObject)ghostModuleToAvailableNodes[moduleUnderMouse]).GetComponent <Renderer> ();
                rend.material.color = colorManager.moduleSelected;

                if (Input.GetMouseButtonDown(0))
                {
                    moduleUnderMouse.name             = FindNextAvailableName();
                    moduleUnderMouse.transform.parent = robot.transform;
                    moduleUnderMouse.tag = "Module";
                    moduleUnderMouse.GetComponent <ModuleController> ().OnSelected(false);
                    moduleUnderMouse.GetComponent <ModuleController> ().SetToTrigger(false);
                    moduleUnderMouse.GetComponent <ModuleController> ().SetMode(1);

                    GameObject node = FindConnectingNode(moduleUnderMouse, (GameObject)ghostModuleToAvailableNodes[moduleUnderMouse]);
                    Connect(node, (GameObject)ghostModuleToAvailableNodes[moduleUnderMouse]);
                }
                if (Input.GetKeyDown(KeyCode.R))
                {
                    moduleUnderMouse.transform.Rotate(moduleUnderMouse.transform.up, 90.0f, Space.World);
                }
            }
        }
        else if (modeManager.IsRecordBehavior)
        {
            if (Input.GetMouseButtonDown(0))
            {
                SelectModule();
            }
        }
        else if (modeManager.IsConnectNodes)
        {
            PlotAvailableNodes(true);
            nodeUnderMouse = FindNodeUnderMouse();
            if (nodeUnderMouse != null)
            {
                Renderer rend = nodeUnderMouse.GetComponent <Renderer> ();
                rend.material.color = colorManager.moduleSelected;
                if (Input.GetMouseButtonDown(0))
                {
                    if (selectedNode1 == null)
                    {
                        selectedNode1 = nodeUnderMouse;
                    }
                    else if ((selectedNode2 == null) && (selectedNode1 != nodeUnderMouse))
                    {
                        selectedNode2 = nodeUnderMouse;
                    }
                    else
                    {
                        selectedNode1 = nodeUnderMouse;
                        selectedNode2 = null;
                    }

                    if (selectedNode2 != null)
                    {
                        if (!CheckDifferentConf(selectedNode1, selectedNode2))
                        {
                            buttonConnect.GetComponent <Button> ().interactable = true;
                            buttonConnectText.text = "Connect two conf";
                        }
                        else
                        {
                            if (CheckNodeAdjacency(selectedNode1, selectedNode2))
                            {
                                string name = selectedNode1.transform.parent.name + ":" + selectedNode1.name;
                                if (!connectionTable.ContainsKey(name))
                                {
                                    buttonConnect.GetComponent <Button> ().interactable = true;
                                    buttonConnectText.text = "Connect";
                                }
                                else
                                {
                                    buttonConnect.GetComponent <Button> ().interactable = true;
                                    buttonConnectText.text = "Disconnect";
                                }
                            }
                            else
                            {
                                buttonConnectText.text = "Not adjacent";
                            }
                        }
                    }
                    else
                    {
                        buttonConnect.GetComponent <Button> ().interactable = false;
                        buttonConnectText.text = "Select 2nd node";
                    }
                }
            }

            if (selectedNode1 != null)
            {
                Renderer rend = selectedNode1.GetComponent <Renderer> ();
                rend.material.color = colorManager.moduleSelected;
            }

            if (selectedNode2 != null)
            {
                Renderer rend = selectedNode2.GetComponent <Renderer> ();
                rend.material.color = colorManager.moduleSelected;
            }
        }
        else
        {
            if (Input.GetMouseButtonDown(0))
            {
                SelectModule();
            }
        }

        if (isDrag)
        {
            float   distCam2Origin = Camera.main.transform.position.magnitude;
            Vector3 newPos         = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distCam2Origin));
            newConf.transform.position = newPos;
            if (Input.GetMouseButtonDown(0))
            {
                saveLoadManagerScript.ConnectAllModules();
                foreach (string m1 in saveLoadManagerScript.connectionTable.Keys)
                {
                    string m2 = (string)saveLoadManagerScript.connectionTable[m1];
                    connectionTable.Add(m1, m2);
                }
                saveLoadManagerScript.newConfCount += 1;
                isDrag = false;
            }
            if (Input.GetKeyDown(KeyCode.R))
            {
                newConf.transform.Rotate(newConf.transform.up, 90.0f, Space.World);
            }
            if (Input.GetKeyDown(KeyCode.Escape))
            {
                isDrag = false;
                Destroy(newConf);
                newConf = new GameObject();
            }
        }
    }