Help with C# script ?

hi guys, i just created a raycast script that opens a door when the hit collider hits the door. The script i made was done in java now the thing is that i now want to take this script and make the same thing in C sharp. But as soon as i tried that i came across a problem, basically in the update function in my java script i created a variable called :

  var Hit: RaycastHit;

now in C sharp if i try to make the same variable it wont work what is the correct way to put this right in C#

here are my scripts :

JAVA:


      var RayCastDist  = 5;

          function Update () {

             var hit : RaycastHit;

           Debug.DrawRay (transform.position, Vector3.forward * RayCastDist, Color.green); 
                if(Physics.Raycast(transform.position,Vector3.forward, hit, RayCastDist))

       {
             Debug.DrawRay (transform.position, Vector3.forward * RayCastDist, Color.red);
                  if(hit.collider.gameObject.tag == "door"){

                        hit.collider.gameObject.animation.Play("Door animation");

      }
    }
   }


That was the Java script here is the C sharp :

    using UnityEngine;
       using System.Collections;

       public class OpenDoorScriptRayCSharp : MonoBehaviour {

             public float RayCastHitDoor  = 5;

         // Update is called once per frame
           void Update () {

       }
      }


So yea thats the scripts and thanks in advance :)

You have a float defined as 5, to increase readability you could use 5.0f instead. This is common practice but a choice nonetheless. (Thanks go to KeithK for spotting the error)

By the way, this is actually not Java at all; rather, it’s JavaScript (more precisely, UnityScript — Unity’s adaptation of JavaScript), which is not the same thing.

1 Answer

1

The way you need to declare that variable in C# is

RaycastHit hit;

No need for the var keyword in C#.

thank you very much :)

No problem. =) Just remember in C# any variable you want to declare is <Type> <Name>

yea thanks :) dude

I couldn't find an instance of var in the C# code, I checked for that the first Thing I possibly could. Am I just reading this question entirely differently ? I see 2 blocks one in Java one in C# , and they seem to be about different things, and a that raycast as the text claims is a java code bit (in my interpretation ) """Quote""" But as soon as i tried that i came across a problem, basically in the update function in my java script i created a variable called : var Hit: RaycastHit; """End Quote""" You are very correct saying don't use var in C# +1 for that