- Home /
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!
Answer by robertbu · Jan 26, 2013 at 06:16 AM
You are missing a ';' on line 19. It should be:
Debug.Log("No collision.");
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
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?A field initializer cannot reference the nonstatic field, method, or property `UnityEngine.Component.transform'
NullReferenceException
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.
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).
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.
I....am confused...your C# script does not have anything about LineRenderer. That could be the reason why. Or am I missing something?
Your answer
Follow this Question
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