Disabling a drag and drop script
Hi. I am creating a game of battleship and cant get my battleships to disable their drag and drop feature when you click start
the following is my drag and drop script
using UnityEngine; using System.Collections;
[RequireComponent(typeof(BoxCollider2D))]
public class DragNDrop : MonoBehaviour {
private Vector3 screenPoint;
private Vector3 offset;
public void OnMouseDown()
{
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
public void OnMouseDrag()
{
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
transform.position = curPosition;
}
}
the following is my disable script. It gets the boolean value of a button i already wrote the script for and created to determine whether or not it should disable the boat.
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class Disable : MonoBehaviour {
public DragNDrop DragNDrop;
public Carrier Carrier;
public Button startB;
void Start()
{
DragNDrop = GetComponent<DragNDrop>();
Carrier = GetComponent<Carrier>();
}
void Update()
{
if (startB.enabled == false)
{
DragNDrop.enabled = false;
Carrier.enabled = false;
}
else
{
if (startB.enabled == true)
{
DragNDrop.enabled = true;
Carrier.enabled = true;
}
}
}
}
any help would be much appreciated. Thanks!
startB.enabled == false
Note: This will deter$$anonymous$$e if the button's scripts are enabled, NOT if the button has been pressed.
Glurth I have that line already. This code is supposed to disable 2 scripts. It disables one perfectly, but for some reason it is having trouble disabling the drag and drop script
It's just odd to disable a button (startB.enabled=false) when it has been clicked on. I say this because you are checking the enabled state of the button, as apposed to the "pressed" state of the button. (assu$$anonymous$$g StartB is the button that controls the enable/disable of the drag&drop behavior.) Perhaps rather than a button, you want to use a "Toggle" (unity class similar to button- looks like a checkbox).
The Toggle class has an "isOn" variable for you to look at to deter$$anonymous$$ed the toggle's state: http://docs.unity3d.com/ScriptReference/UI.Toggle-isOn.html I would expect the toggle to remain "enabled".