I’m doing a candy crush like game. When I drag a “candy” I want the all mouse interaction to stop so that other "candy"s cannot be touched or moved
I am using a variable to block interaction in the OnMouseDrag and OnMouseDown but it doesn’t seem to be working
public class LightBehavior : MonoBehaviour {
//public float speed = 0.01F;
public GameObject block;
private Text text;
private float speed = 0.004f;//10.0f;
private Vector3 initialPosition;
private GameObject clone;
static bool dragging = false;
//private string selected="";
private static float switchSpeed = 4;
//Vector3 dist;
//float posX;final
//float posY;final
static GameObject objPrefab;
Vector3 MyPosition;
//float yy=1;
// Use this for initialization
// Update is called once per frame
IEnumerator switchL(GameObject s, GameObject t, GameObject cs, GameObject ct )
{
while (s && t && cs&& ct && (s.transform.position.x != ct.transform.position.x || s.transform.position.y != ct.transform.position.y
|| t.transform.position.x != cs.transform.position.x || t.transform.position.y != cs.transform.position.y))
{
s.transform.position = Vector3.MoveTowards (s.transform.position, ct.transform.position, switchSpeed * Time.deltaTime);
t.transform.position = Vector3.MoveTowards (t.transform.position, cs.transform.position, switchSpeed * Time.deltaTime);
yield return null;
}
//BlockBehavior.occurringAnimations--;
TrashMan.despawn(cs);
TrashMan.despawn(ct);
BlockBehavior bb = block.GetComponent<BlockBehavior>();
if (!bb.Interact (s.name, t.name))
{
cs = cloneLight (s);
ct = cloneLight (t);
StartCoroutine (switchBack (s, t, cs, ct));
}
//s.collider.enabled = true;
//print (DisableSelect);
//print (DisableDrag);
}
IEnumerator switchBack(GameObject s, GameObject t, GameObject cs, GameObject ct)
{
BlockBehavior.occurringAnimations++;
while (s.transform.position.x != ct.transform.position.x || s.transform.position.y != ct.transform.position.y
|| t.transform.position.x != cs.transform.position.x || t.transform.position.y != cs.transform.position.y)
{
s.transform.position = Vector3.MoveTowards (s.transform.position, ct.transform.position, switchSpeed * Time.deltaTime);
t.transform.position = Vector3.MoveTowards (t.transform.position, cs.transform.position, switchSpeed * Time.deltaTime);
yield return null;
}
TrashMan.despawn(cs);
TrashMan.despawn(ct);
///Destroy (cs);
///Destroy (ct);
BlockBehavior.occurringAnimations--;
BlockBehavior.occurringAnimations--; //coz we ddnt decrement for the switchL
s.GetComponent<Collider>().enabled = true;
t.GetComponent<Collider>().enabled = true;
}
void OnMouseDown()
{
//print (name);
//transform.Translate (Vector3.down*Time.smoothDeltaTime*100, Space.Self);
if(!dragging && !BlockBehavior.DisableSelect && !BlockBehavior.disableDrag && BlockBehavior.occurringAnimations ==0)
{
BlockBehavior.DisableSelect = true;
//secondLight = "";
/*
BlockBehavior bb = block.GetComponent<BlockBehavior> ();
GameObject light = bb.getLight (name);
print (light.name);
bb.getRight (name);
bb.getLeft (name);
bb.getUp (name);
bb.getDown (name);*/
//selected = name;
//Debug.Log(name);
//dist = Camera.main.WorldToScreenPoint(transform.position);final
//posX = Input.mousePosition.x - dist.x;final
//posY = Input.mousePosition.y - dist.y;final
//clone = GameObject.Find("Clone-" + name);
if(GameObject.Find("Clone-" + name))//clone)
{
TrashMan.Destroy(clone);
// TrashMan.despawn(ct);
// Destroy ();
}
clone = cloneLight (gameObject);
//clone.name = "sikish";
//transform.position = new Vector3(dist.x,dist.y,-2f);
}
}
public static GameObject cloneLight(GameObject l)
{
GameObject _clone;
BlockBehavior.recycle.RemoveAll (item => item == null);
if (BlockBehavior.recycle.Count > 0)
{
_clone = BlockBehavior.recycle.First();
//_clone.GetComponent<Rigidbody> ().useGravity = true;
_clone.SetActive (true);
BlockBehavior.recycle.Remove (_clone);
}
else
{
_clone = (GameObject)Instantiate (objPrefab);
}
_clone.name = "Clone-" + l.name;
_clone.transform.parent = l.gameObject.transform.parent;
_clone.transform.position = new Vector3 (l.transform.position.x, l.transform.position.y, l.transform.position.z);
_clone.GetComponent<SpriteRenderer> ().enabled = false;
//_clone.GetComponent<SphereCollider> ().enabled = false;
_clone.GetComponent<Collider>().enabled = false;
//Destroy (_clone, 2.0f);
//LightBehavior lb = _clone.GetComponent<LightBehavior> ();final
//lb.block = BlockBehavior.block;
//lb.RemoveAfter ();
TrashMan.despawnAfterDelay (_clone, 3.0f);
return _clone;
}
void OnMouseDrag()
{
//print ("disabled: " + BlockBehavior.DisableDrag);
//print ("number of animations: "+ BlockBehavior.occurringAnimations);
if(!dragging && !BlockBehavior.DisableDrag && BlockBehavior.occurringAnimations ==0)
{
BlockBehavior.DisableDrag = true;
BlockBehavior.DisableSelect = true;
//Vector3 curPos = new Vector3(Input.mousePosition.x - posX, Input.mousePosition.y - posY, dist.z); final
//get interacting light
GameObject intLight = null;
BlockBehavior bb = block.GetComponent<BlockBehavior>();
//GameObject
float x = Input.GetAxis ("Mouse X") , y = Input.GetAxis ("Mouse Y");
if ( Mathf.Abs(x) > Mathf.Abs (y))
{
if (x >0)
{
//Debug.Log ("right");
intLight = bb.getRight(name);
}
else
{
//Debug.Log("left");
intLight = bb.getLeft(name);
}
}
else if ( Mathf.Abs(x) < Mathf.Abs (y))
{
if (y >0)
{
//Debug.Log ("up");
intLight = bb.getUp(name);
}
else
{
//Debug.Log("down");
intLight = bb.getDown(name);
}
}
else
{
//Debug.Log ("mouse: x = " + x + " Y = " + y);
//BlockBehavior.DisableDrag = false;
}
//Debug.Log(clone.name);
if(intLight)
{
dragging = true;
GameObject cInt = cloneLight (intLight);
//gameObject.collider.enabled = false;
try
{
BlockBehavior.occurringAnimations++;
StartCoroutine (switchL (gameObject,intLight , clone , cInt));
}
catch(Exception ex)
{
Debug.Log(ex+" something bad happened with source: " + gameObject.name + " " + gameObject.tag + " - " +intLight.name + " " + intLight.tag);
}
/*if(!bb.Interact(gameObject.name,intLight.name))
{
clone = cloneLight(gameObject);
cInt = cloneLight(intLight);
StartCoroutine (switchL (gameObject,intLight , clone , cInt));
}*/
}
else
{
//DisableDrag = false;
//DisableSelect = false;
}
//gameObject.collider.enabled = false;
/*ClampPositionToCircle (dist, 40f, ref curPos);
Vector3 worldPos = Camera.main.ScreenToWorldPoint(curPos);
transform.position = worldPos;
//print (gameObject);*/
}
}
void OnMouseUp()
{
dragging = false;
/*
*
gameObject.transform.position = new Vector3(clone.transform.position.x,clone.transform.position.y,clone.transform.position.z);
Destroy (clone);
BlockBehavior bb = block.GetComponent<BlockBehavior>();
if(secondLight != "")
{
bb.Interact(gameObject.name,secondLight);
}*/
}
public void RemoveMe()
{
//Destroy (gameObject);
//print (gameObject.name + " " + gameObject.tag);
//gameObject.GetComponent<SpriteRenderer>().enabled = false;
//gameObject.GetComponent<SphereCollider> ().enabled = false;
//BlockBehavior bb = block.GetComponent<BlockBehavior> ();
BlockBehavior.beingDestroyed.Remove (name);
//print (BlockBehavior.occurringAnimations);
//Destroy (GameObject.Find ("Clone-" + gameObject.name));
//Destroy (gameObject);
if (BlockBehavior.recycle.Count > 0)
{
//edit
// BlockBehavior.recycleLight (gameObject);
//block = null;
}
else
TrashMan.despawn(gameObject);
//Destroy (gameObject);
//bb.getResultantChains ();
}
}