Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Raycast problem!

Discussion in 'Scripting' started by DrunkProgrammer, Oct 7, 2015.

  1. DrunkProgrammer

    DrunkProgrammer

    Joined:
    Mar 7, 2015
    Posts:
    12
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RayCastScript : MonoBehaviour {
    5.  
    6.     static float distance1 =5;
    7.  
    8.     void Update() {
    9.         RaycastHit hit;
    10.         Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), hit);
    11.         distance1 = hit.distance;
    12.     }
    13. }
    14.  
    Assets/Scripts/RayCastScript.cs(10,50): error CS1525: Unexpected symbol `<internal>'

    Does anyone knows what is wrong with the above code?
     
  2. Thomas-Mountainborn

    Thomas-Mountainborn

    Joined:
    Jun 11, 2015
    Posts:
    489
    The hit variable needs to be passed as "out":
    Code (csharp):
    1. Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit);
    Also, as a side note, you could use "transform.forward" directly, instead of transform.TransformDirection(Vector3.forward).
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Well, that is an unusually unhelpful error message. But most likely, the issue is that 'hit' should be 'out hit', as it's an output parameter.
     
  4. DrunkProgrammer

    DrunkProgrammer

    Joined:
    Mar 7, 2015
    Posts:
    12
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RayCastScript : MonoBehaviour {
    5.  
    6.     static float distance1 = 5;
    7.  
    8.     void Update()  {
    9.         RaycastHit hit;
    10.         Physics.Raycast(transform.position, transform.forward, out hit);
    11.         distance1 = hit.distance;
    12.     }
    13. }
    I changed the code to that but the problem consists!
     
  5. Thomas-Mountainborn

    Thomas-Mountainborn

    Joined:
    Jun 11, 2015
    Posts:
    489
    Hmm, there's something strange going on in that file then. Try making a new script and pasting those statements in there, see if the problem persists.
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Something strange is going on. Since I've never seen that error before, I copied and pasted your code into a CS file, and it compiled without a problem. This suggests to me that something weird is going on with either the file or the project. Can you describe anything out of the ordinary you might have in your project? Any packages or plugins, have you played with the player settings, etc, etc? Oh, and does your script have the correct extension (cs)?
     
  7. DrunkProgrammer

    DrunkProgrammer

    Joined:
    Mar 7, 2015
    Posts:
    12
    I copied the code to another script but I get the same error also I have the default settings of unity and the script's extension is .cs
     
  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Well.... you appear to have actually uncovered a bug in Unity. Before you do anything else, submit a bug report with this project file.

    And I'm not sure what else the rest of us will be able to say to help in this scenario - you have a situation I haven't ever seen, and can't reproduce. Sorry :/
     
  9. Thomas-Mountainborn

    Thomas-Mountainborn

    Joined:
    Jun 11, 2015
    Posts:
    489
    Do you get the error with any other scripts / projects? Maybe something broke in your Unity install. If you do get the error with other files as well, it might be worth trying a clean install of Unity.
     
  10. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987