I wrote this code, but there is one problem. This code works differently on different platforms. On Android it works without errors. But on PC it gives an error AssetsScriptsHingeMovement.cs(119,2): error CS1513: } expected
. What should I do please tell me.
This code was designed to hook onto objects and use them to move.
On Android
On PC
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class HingeMovement : MonoBehaviour
{
private bool _grabbing;
public LineRenderer lr;
public GameObject button;
GameObject FindNearest()
{
GameObject[] hinges;
hinges = GameObject.FindGameObjectsWithTag("hinge");
GameObject closest = null;
float distance = Mathf.Infinity;
Vector3 position = transform.position;
foreach (GameObject go in hinges)
{
Vector3 diff = go.transform.position - position;
float curDistance = diff.sqrMagnitude;
if (curDistance < distance)
{
closest = go;
distance = curDistance;
}
}
return closest;
}
void Update ()
{
lr.startWidth = 0.08f;
lr.endWidth = 0.08f;
lr.startColor = Color.yellow;
lr.endColor = Color.yellow;
#if UNITY_STANDALONE
button.SetActive(false);
if (Input.GetMouseButtonDown(0))
{
_grabbing = true;
}
if (Input.GetMouseButton(0))
{
lr.positionCount = 2;
GameObject closest = FindNearest();
if (_grabbing)
{
lr.SetPosition(1, closest.transform.position);
closest.GetComponentInChildren<HingeJoint2D>().connectedBody =
gameObject.GetComponentInChildren<Rigidbody2D>();
_grabbing = false;
}
lr.SetPosition(0, transform.position);
}
if (Input.GetMouseButtonUp(0))
{
GameObject[] hinges;
hinges = GameObject.FindGameObjectsWithTag("hinge");
foreach (GameObject go in hinges)
{
lr.positionCount = 0;
go.GetComponentInChildren<HingeJoint2D>().connectedBody = null;
}
if (Input.GetMouseButton(1)) //ЛКМ + ПКМ
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 0);
}
}
#elif UNITY_ANDROID
button.SetActive(true);
if (Input.touchCount > 0)
{
_grabbing = true;
}
if (Input.touchCount == 1)
{
lr.positionCount = 2;
GameObject closest = FindNearest();
if (_grabbing)
{
lr.SetPosition(1, closest.transform.position);
closest.GetComponentInChildren<HingeJoint2D>().connectedBody =
gameObject.GetComponentInChildren<Rigidbody2D>();
_grabbing = false;
}
lr.SetPosition(0, transform.position);
}
if (Input.touchCount == 0)
{
GameObject[] hinges;
hinges = GameObject.FindGameObjectsWithTag("hinge");
foreach (GameObject go in hinges)
{
lr.positionCount = 0;
go.GetComponentInChildren<HingeJoint2D>().connectedBody = null;
}
}
}
public void PhoneRestart()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 0);
}
#endif
}
I think it’s the placement of the words #if UNITY_STANDALONE
, #elif UNITY_ANDROID
and #endif
.
I need this code to work on these two platforms