Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Alter · May 15, 2014 at 06:53 AM · c#errorraycastrts

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;
         }
     } 
 }
 
 
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Parzivell · May 15, 2014 at 07:05 AM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by murkertrer · May 15, 2014 at 07:37 AM

Yes, share the console messages ^^

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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>

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

cant kill more than one enemy C# 2 Answers

Raycast doesn't collide as it should 1 Answer

How to "Camera.ScreenPointToRay". I get an error in MonoDevelop. 1 Answer

enemy detect player then attack - c# 1 Answer

NullReferenceError, Tilemap Array with Transforms and Raycast (C# with Demo) 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges