- Home /
Errors, Parsing Errors everywhere :( please help
I have spent way to much time on this and it's doing my head in- can someone please help me fix this broken code. -_- Many thanks.
using UnityEngine; using System.Collections;
public class Mouse : MonoBehaviour {
RaycastHit hit;
public static GameObject CurrentlySelectedUnit;
public GameObject Target;
private Vector3 mouseDownPoint;
// Use this for initialization
void Awake ()
{
mouseDownPoint = Vector3.zero;
}
// Update is called once per frame
void Update ()
{
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if(Physics.Raycast(ray, out hit, Mathf.Infinity))
{
//store point at mouse button down
if(Input.GetMouseButtonDown(0))
{
mouseDownPoint = hit.point;
//Debug.Log (hit.collider.name);
if(hit.collider.name == "Floor")
{
if(Input.GetButtonDown(1))
{
GameObject TargetObj = Instantiate(Target, hit.point, Quaternion.identity) as GameObject;
TargetObj.name = "Target Instantiaed";
}
}
else if (Input.GetMouseButtonUp(0) && DidUserClickLeftMouse(mouseDownPoint))
DeselectGameObjectIfSelected();
}
}
}//end of the terrain!
else {
//hitting other objects!
if (Input.GetMouseButtonUp(0) && DidUserClickLeftMouse(mouseDownPoint))
{
//is the user hitting unit?
if(hit.collider.transform.FindChild("Selected"))
{
//found unit to select!
Debug.Log ("Found Unit!");
//are we selecting a different object?
if(CurrentlySelectedUnit != hit.collider.gameObject)
{
//activate the selector
GameObject SelectedObj = hit.collider.transform.FindChild("Selected");
SelectedObj.active = true;
//deactivate the currently selected objects selector
if(CurrentlySelectedUnit != null)
CurrentlySelectedUnit.transform.FindChild("Selected").gameObject.active = false;
//replace currently selected unit
CurrentlySelectedUnit = hit.collider.gameObject;
}
} else {
//if this object is not a unit
DeselectGameObjectIfSelected();
}
}
}
} else {
if (Input.GetMouseButtonUp(0) && DidUserClickLeftMouse(mouseDownPoint))
DeselectGameObjectIfSelected();
}
Debug.DrawRay(ray.origin, ray.direction * mathf.Infinity, Color.yellow);
}
//check if user clicked
public bool (DidUserClickLeftMouse(Vector3) hitPoint)
{
float clickZone = 1.3f;
if(
(mouseDownPoint.x < hitPoint.x + clickZone && mouseDownPoint.x > hitPoint.x - clickZone) &&
(mouseDownPoint.y < hitPoint.y + clickZone && mouseDownPoint.y > hitPoint.y - clickZone) &&
(mouseDownPoint.z < hitPoint.z + clickZone && mouseDownPoint.z > hitPoint.z - clickZone)
)
return true; else return false;
}
public static void DeselectGameObjectIfSelected()
{
if(CurrentlySelectedUnit != null)
{
CurrentlySelectedUnit.transform.FindChild("Selected").gameObject.activeSelf = false;
CurrentlySelectedUnit = null;
}
}
}
It would be great to give us the errors in the Unity Console, this could help us all out and make an answer alot quicker
Answer by adsamcik · May 15, 2014 at 07:13 AM
There aren't that many issue but some of them are major. You should keep your code cleaner I had to use cleanup tool to be able to fix it, kind of.Probably you should also consider editor that will help you with unity syntax (I would recommend built in mono, it should be fine for start, it also shows whats wrong).
It's not completely fixed, look at double else I mentioned, line 43 (else { //here should be else if)
but everything else should be fine and look at last function, I hope I fixed it the way you intended it to be.
using UnityEngine; using System.Collections;
public class Mouse : MonoBehaviour
{
RaycastHit hit;
public static GameObject CurrentlySelectedUnit;
public GameObject Target;
private Vector3 mouseDownPoint;
// Use this for initialization
void Awake()
{
mouseDownPoint = Vector3.zero;
}
// Update is called once per frame
void Update() {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, Mathf.Infinity)) {
//store point at mouse button down
if (Input.GetMouseButtonDown(0)) {
mouseDownPoint = hit.point;
//Debug.Log (hit.collider.name);
if (hit.collider.name == "Floor") {
if (Input.GetKeyDown(KeyCode.Alpha1)) {
GameObject TargetObj = Instantiate(Target, hit.point, Quaternion.identity) as GameObject;
TargetObj.name = "Target Instantiaed";
}
} else if (Input.GetMouseButtonUp(0) && DidUserClickLeftMouse(mouseDownPoint))
DeselectGameObjectIfSelected();
}
}
//end of the terrain!
else { //here should be else if
//hitting other objects!
if (Input.GetMouseButtonUp(0) && DidUserClickLeftMouse(mouseDownPoint)) {
//is the user hitting unit?
if (hit.collider.transform.FindChild("Selected")) {
//found unit to select!
Debug.Log("Found Unit!");
//are we selecting a different object?
if (CurrentlySelectedUnit != hit.collider.gameObject) {
//activate the selector
Transform SelectedObj = hit.collider.transform.FindChild("Selected");
SelectedObj.gameObject.SetActive(true);
//deactivate the currently selected objects selector
if (CurrentlySelectedUnit != null)
CurrentlySelectedUnit.transform.FindChild("Selected").gameObject.SetActive(false);
//replace currently selected unit
CurrentlySelectedUnit = hit.collider.gameObject;
}
} else {
//if this object is not a unit
DeselectGameObjectIfSelected();
}
}
} else {
if (Input.GetMouseButtonUp(0) && DidUserClickLeftMouse(mouseDownPoint))
DeselectGameObjectIfSelected();
}
Debug.DrawRay(ray.origin, ray.direction * Mathf.Infinity, Color.yellow);
}
//check if user clicked
public bool DidUserClickLeftMouse(Vector3 hitPoint)
{
float clickZone = 1.3f;
if ((mouseDownPoint.x < hitPoint.x + clickZone && mouseDownPoint.x > hitPoint.x - clickZone) &&
(mouseDownPoint.y < hitPoint.y + clickZone && mouseDownPoint.y > hitPoint.y - clickZone) &&
(mouseDownPoint.z < hitPoint.z + clickZone && mouseDownPoint.z > hitPoint.z - clickZone))
return true;
else return false;
}
public static void DeselectGameObjectIfSelected()
{
if (CurrentlySelectedUnit != null)
{
//CurrentlySelectedUnit.transform.FindChild("Selected").gameObject.activeSelf = false; activeself is static value cannot be assigned
CurrentlySelectedUnit.transform.FindChild("Selected").gameObject.SetActive(false);
CurrentlySelectedUnit = null;
}
}
}</code></pre>