Hello everyone. Right now I am working on a script to select a game object on right click and I'm having an issue with the right click raycasting selection script. If anyone could take a look I'd greatly appreciate it. Code (CSharp): using UnityEngine; using System.Collections; public class RightClickSelector : MonoBehaviour { GameObject activeObject; void Update () { if (Input.GetMouseButtonDown(1)){ Ray ray = camera.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit)){ activeObject =(hit.transform.gameObject); } } }
you have 4 opening brackets, and 3 closing brackets... short 1 closing bracket parsing errors means the syntax is bad, so check the brackets etc.
I tried that and it still doesn't work. I rephrased the lines and it's still giving the same error. Code (CSharp): using UnityEngine; using System.Collections; using System.Collections.Generic; public class RightClickSelect : MonoBehaviour { void Update(){ if (Input.GetMouseButtonDown(1)){ // if rightbutton pressed... Ray ray = camera.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit)){ GameObject activeObject = (hit.transform.gameObject); } else { GameObject activeObject = null; } } }
you've not added another closing bracket, all you've done is add an else clause to the if. COUNT the opening and closing brackets. Also, if you correctly formatted the code you would immediately see the issue. Code (csharp): using UnityEngine; using System.Collections; using System.Collections.Generic; public class TestScript : MonoBehaviour { // open 1 void Update() { // open 2 if (Input.GetMouseButtonDown(1)) { // open 3 Ray ray = camera.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit)) { // open 4 GameObject activeObject = (hit.transform.gameObject); } // close 4 else { // open 5 GameObject activeObject = null; } // close 5 } //close 3 } //close 2 // where is close 1?!?!? once that syntax error is fixed you'll run into the fact that you've done line 11 incorrectly, should be: Code (csharp): Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); vs at the very least is putting in a red squiggle at the end of the file where the missing bracket is meant to be