- Home /
Mouse Methods (OnMouseDown, OnMouseOver etc) stop working after certain circumstances
in my game you can move on units, and nothing in between.
you can also right click blocks to delete them.
there is also another way to do this, by hitting the - button, but that just deletes the current block.
when i delete the current block by right click, then add it back with the + button, that new block ignores mouse functions.
cube script:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Cube : MonoBehaviour {
public GameObject CubeManagerGet;
public GameObject AddButtonGet;
public GameObject SubtractButtonGet;
public Text TextGet;
public int DataValue;
public bool CanSubtract;
void Start () {
CubeManagerGet.GetComponent <CubeManager> ().Cubes.Add (gameObject);
DataValue = CubeManagerGet.GetComponent <CubeManager> ().Cubes.Count - 1;
if (AddButtonGet.GetComponent <ActionButton> ().HasReset == true)
{
CubeManagerGet.GetComponent <CubeManager> ().CurrentCube = DataValue;
AddButtonGet.GetComponent <ActionButton> ().HasReset = false;
}
if (DataValue == 0)
{
GetComponent <SpriteRenderer> ().color = Color.red;
GetComponent <SpriteRenderer> ().sortingOrder = 2;
}
else
{
GetComponent <SpriteRenderer> ().color = Color.black;
GetComponent <SpriteRenderer> ().sortingOrder = 1;
}
gameObject.name = "Cube (" + DataValue + ")";
}
void Update () {
Debug.Log (CanSubtract);
if (CubeManagerGet.GetComponent <CubeManager> ().CurrentCube == DataValue)
{
GetComponent <SpriteRenderer> ().sortingOrder = 2;
GetComponent <SpriteRenderer> ().color = Color.red;
TextGet.text = "Coordinates: {" + transform.position.x + ", " + transform.position.y + "}";
TextGet.color = Color.green;
}
else
{
GetComponent <SpriteRenderer> ().sortingOrder = 1;
GetComponent <SpriteRenderer> ().color = Color.black;
}
if (CanSubtract == true && Input.GetMouseButton (1) && CubeManagerGet.GetComponent <CubeManager> ().Cubes [CubeManagerGet.GetComponent <CubeManager> ().CurrentCube] != null && CubeManagerGet.GetComponent <CubeManager> ().CubeCount > 1)
{
if (CubeManagerGet.GetComponent <CubeManager> ().CurrentCube == DataValue)
{
TextGet.text = "No Cube Selected";
TextGet.color = Color.red;
}
Destroy (gameObject);
}
if (Input.GetKey (KeyCode.LeftShift) && CubeManagerGet.GetComponent <CubeManager> ().CurrentCube == DataValue && CubeManagerGet.GetComponent <CubeManager> ().Cubes [CubeManagerGet.GetComponent <CubeManager> ().CurrentCube] != null)
{
if (Input.GetKeyDown (KeyCode.W))
{
Instantiate (gameObject, new Vector3 (transform.position.x, transform.position.y + 1, transform.position.z), Quaternion.identity);
}
if (Input.GetKeyDown (KeyCode.A))
{
Instantiate (gameObject, new Vector3 (transform.position.x - 1, transform.position.y, transform.position.z), Quaternion.identity);
}
if (Input.GetKeyDown (KeyCode.S))
{
Instantiate (gameObject, new Vector3 (transform.position.x, transform.position.y - 1, transform.position.z), Quaternion.identity);
}
if (Input.GetKeyDown (KeyCode.D))
{
Instantiate (gameObject, new Vector3 (transform.position.x + 1, transform.position.y, transform.position.z), Quaternion.identity);
}
}
}
void OnMouseOver () {
CanSubtract = true;
}
void OnMouseExit () {
CanSubtract = false;
}
void OnMouseDown () {
CubeManagerGet.GetComponent <CubeManager> ().CurrentCube = DataValue;
}
}
button script:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ActionButton : MonoBehaviour {
public enum Actions {HorizontalNegative, VerticalNegative, HorizontalPositive, VerticalPositive, Add, Subtract};
public Actions Action;
public GameObject CubeManagerGet;
public GameObject CubeToEdit;
public Text TextGet;
public bool HasReset;
public void Interact () {
CubeToEdit = CubeManagerGet.GetComponent <CubeManager> ().Cubes [CubeManagerGet.GetComponent <CubeManager> ().CurrentCube];
if (Action == Actions.VerticalPositive)
{
CubeToEdit.GetComponent <Cube> ().transform.position += Vector3.up;
}
if (Action == Actions.HorizontalNegative)
{
CubeToEdit.GetComponent <Cube> ().transform.position += Vector3.left;
}
if (Action == Actions.VerticalNegative)
{
CubeToEdit.GetComponent <Cube> ().transform.position += Vector3.down;
}
if (Action == Actions.HorizontalPositive)
{
CubeToEdit.GetComponent <Cube> ().transform.position += Vector3.right;
}
if (Action == Actions.Add)
{
if (CubeManagerGet.GetComponent <CubeManager> ().Cubes [CubeManagerGet.GetComponent <CubeManager> ().CurrentCube] != null)
{
Instantiate (CubeToEdit, CubeToEdit.transform.position, Quaternion.identity);
}
else
{
for (int CubeCheck = 0; CubeCheck < CubeManagerGet.GetComponent <CubeManager> ().CubeCount - 1; CubeCheck ++)
{
if (CubeManagerGet.GetComponent <CubeManager> ().Cubes [CubeCheck] != null)
{
CubeToEdit = CubeManagerGet.GetComponent <CubeManager> ().Cubes [CubeCheck];
HasReset = true;
Instantiate (CubeToEdit, Camera.main.transform.position, Quaternion.identity);
break;
}
}
}
}
if (Action == Actions.Subtract && CubeManagerGet.GetComponent <CubeManager> ().CubeCount > 1)
{
Destroy (CubeManagerGet.GetComponent <CubeManager> ().Cubes [CubeManagerGet.GetComponent <CubeManager> ().CurrentCube]);
TextGet.text = "No Cube Selected";
TextGet.color = Color.red;
}
}
}
Comment
Just checked, and FYI the clones made that way do in fact have colliders.
Your answer
Follow this Question
Related Questions
Adding a life on collision, otherwise subtract 1 Answer
Instantiate prefab and Add to a Generic List 2 Answers
Only 1 of 3 conditions being executed in IF statement?(Solved) 1 Answer
Can't seem to destroy instantiated objects. 1 Answer
Can a Network.Instantiate()'d object be Object.Destroy()'d? 0 Answers