x


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 :)

more ▼

asked Apr 08 '11 at 12:06 PM

MC HALO gravatar image

MC HALO
878 74 94 111

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)

Apr 08 '11 at 12:49 PM Proclyon

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.

Feb 28 '12 at 09:27 PM ti89TProgrammer
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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

RaycastHit hit;

No need for the var keyword in C#.

more ▼

answered Apr 08 '11 at 12:26 PM

KeithK gravatar image

KeithK
862 2 3 13

thank you very much :)

Apr 08 '11 at 12:29 PM MC HALO

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

Apr 08 '11 at 12:33 PM KeithK

yea thanks :) dude

Apr 08 '11 at 12:46 PM MC HALO

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

Apr 08 '11 at 12:47 PM Proclyon
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1704
x1534
x161

asked: Apr 08 '11 at 12:06 PM

Seen: 1186 times

Last Updated: Feb 28 '12 at 09:27 PM