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 Taryndactyl · Jan 26, 2013 at 05:50 AM · c#raycastparsing error

C# Parsing Error and Unexpected Symbol

I continue to get there errors with no idea as to why.

Assets/Scripts/Laser.cs(22,1): error CS8025: Parsing error

Assets/Scripts/Laser.cs(20,17): error CS1525: Unexpected symbol `}'

My script is named Laser.cs

Here's the code:

 using UnityEngine;
 using System.Collections;
  
 public class Laser: MonoBehaviour 
 {
     private Ray ray;
     private RaycastHit hit = new RaycastHit();
     private Transform Trans;
     void LateUpdate () 
     {
         ray = new Ray(Trans.position, Vector3.left);
         if(Physics.Raycast(ray.origin, ray.direction, out hit, 10))
         {
             Debug.Log(hit.collider.name);
             Debug.Log(hit.distance);
         }
         else
         {
             Debug.Log("No collision.")
         }
     }
 }

Any help would be greatly appreciated.

Also, this is my first post, hello everyone!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · Jan 26, 2013 at 06:16 AM

You are missing a ';' on line 19. It should be:

Debug.Log("No collision.");

Comment
Add comment · Show 10 · 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 Taryndactyl · Jan 26, 2013 at 05:16 PM 0
Share

Wow thank you very much. It's the little things that slip my $$anonymous$$d.

Now I have an alert saying that Trans remains null and is never used. So I tried changing line 8 from...

private Transform Trans;

to

private Transform Trans = transform.position;

and now I get the errors

A field initializer cannot reference the nonstatic field, method, or property `UnityEngine.Component.transform'

NullReferenceException

UnityEngine.Transform.get_position () Laser.LateUpdate () (at Assets/Scripts/Laser.cs:11) I assume the last one is just telling me that Trans is null. Any idea?
avatar image fafase · Jan 26, 2013 at 05:35 PM 0
Share

Trans is never getting anything. You need

 void Start(){
   Trans = GetComponent<Transform>();
 }


Also, you should avoid cap letter for variables, at least Unity uses Cap letter for methods, you should stick with the convention but it is not a law.

avatar image fafase · Jan 26, 2013 at 05:41 PM 0
Share

Finally, when you can make a variable as local as possible:

 using UnityEngine;
 using System.Collections;
  
 public class Laser: $$anonymous$$onoBehaviour{
   private Transform trans;
   void Start(){
       trans = GetComponent<Transform>();
   }
   void LateUpdate (){
     Ray ray = new Ray(trans.position, Vector3.left);
     RaycastHit hit;
     if(Physics.Raycast(ray.origin, ray.direction, out hit, 10)){
       Debug.Log(hit.collider.name);
       Debug.Log(hit.distance);
     }else{
       Debug.Log("No collision.")
     }
   }
 }

The raycast variable does not require to be instantiated (the new Raycast part) since the Raycast function will give it a new value (it is the out keyword).

avatar image Taryndactyl · Jan 26, 2013 at 06:11 PM 0
Share

Oh wow thanks again! Sorry for asking so many trivial questions I am not yet good at program$$anonymous$$g with Unity.

For some reason my LineRenderer is not being drawn anymore. The log will log "No collision" correctly so the data is there but the actual rendering is not happening. $$anonymous$$aybe I have left something out?

Jscript version: (works perfectly)

 var color1: Color = Color.red;
 var color2: Color = Color.red;
 var lineRenderer: LineRenderer;
 
 function Start ()
 {
     lineRenderer = gameObject.AddComponent(LineRenderer);
     lineRenderer.material =  new $$anonymous$$aterial(Shader.Find("Particles/Additive"));
     lineRenderer.SetColors(color1, color2);
     lineRenderer.SetWidth(.01,.01);
     lineRenderer.SetVertexCount(2);
 }
 
 function LateUpdate ()
 {
     var origin = transform.position;
     var direction = transform.forward;
     var endPoint = origin + direction * 5;
     var hit: RaycastHit;
             lineRenderer.SetPosition(0, origin);
             
     if (Physics.Raycast(origin, direction, hit))
         endPoint = hit.point;
         lineRenderer.SetPosition(1, endPoint);
 }

C# version: (having problems)

 using UnityEngine;
 using System.Collections;
  
 public class Laser: $$anonymous$$onoBehaviour 
 {
     private Transform trans;
     
     void Start()
     {
         trans = GetComponent<Transform>();
     }
     
     void LateUpdate ()
     {
         Ray ray = new Ray(trans.position, Vector3.left);
         RaycastHit hit;
         if(Physics.Raycast(ray.origin, ray.direction, out hit, 20))
         {
             Debug.Log(hit.collider.name);
             Debug.Log(hit.distance);
         }
         else
         {
             Debug.Log("No collision.");
         }
     }
 }

Again, thanks, you're awesome for helping a newb.

avatar image fafase · Jan 26, 2013 at 07:54 PM 0
Share

I....am confused...your C# script does not have anything about LineRenderer. That could be the reason why. Or am I missing something?

Show more comments

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

11 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

Related Questions

Distribute terrain in zones 3 Answers

Gun locator script isnt working 1 Answer

how to make one script change a variable in another scipt 2 Answers

Multiple Cars not working 1 Answer

accesing scripts throu raycasthit 1 Answer


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