- Home /
Need help converting js to C#
hI! i got a strange result after conversing js-to-C# on mouse action... so is this two codes same?
Js code:
#pragma strict
#pragma implicit
#pragma downcast
static var destroyed:int=0;
public var container :GameObject;
var scoreParticle:GameObject;
function Update()
{
if (Input.GetButtonDown("Fire1"))
{
var ray = GetComponent.<Camera>().ScreenPointToRay (Input.mousePosition); //ray from camera to mouse position, we use this for clicking on balls
var hit : RaycastHit;
if ( Physics.Raycast (ray, hit, 30.0) ) //if we hit something
{
if(hit.rigidbody)
{ //and it is rigidbody
object=hit.collider.transform.position; // hitted object's position
hit.collider.gameObject.GetComponent.<chack>().Start(); //enable hitted object's chack script,
if(destroyed <= 0 && (int.Parse(container.GetComponent(instantiate).score) >= 10))
{ //if we clicked and don't destroyed any balls
var minusScore=Instantiate(scoreParticle, object - Vector3(0,0,0.3), Quaternion.identity) as GameObject; //instantiate scoreParticle
container.GetComponent(instantiate).score=""+(int.Parse(container.GetComponent(instantiate).score) - 30); //minus 10 to score text
}
}
}
}
}
C# code:
using UnityEngine;
using System.Collections;
public class test : MonoBehaviour
{
static int destroyed=0;
public GameObject container;
GameObject scoreParticle;
private Vector3 obj;
void Update ()
{
if (Input.GetButtonDown("Fire1"))
{
Ray ray= GetComponent<Camera>().ScreenPointToRay(Input.mousePosition); //ray from camera to mouse position, we use this for clicking on balls
//Ray ray = camera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if ( Physics.Raycast (ray,out hit, 30.0f) ) //if we hit something
{
if(hit.rigidbody)
{ //and it is rigidbody
obj=hit.collider.transform.position; // hitted object's position
hit.collider.gameObject.GetComponent<chack>().Start(); //enable hitted object's chack script,
if(destroyed <= 0 && (int.Parse(container.GetComponent<instantiate>().score) >= 10))
{ //if we clicked and don't destroyed any balls
GameObject minusScore=(GameObject)Instantiate(scoreParticle, obj - new Vector3(0,0,0.3f), Quaternion.identity); //as GameObject; //instantiate scoreParticle
container.GetComponent<instantiate>().score=""+(int.Parse(container.GetComponent<instantiate>().score) - 30); //minus 10 to score text
}
}
}
}
}
}
Cause in js script used is "hit", but in C# i have to use "out hit"
Answer by Kalmak · Oct 14, 2012 at 03:06 PM
I didn't notice anything wrong at a glance. Here is some info on the out keyword and passing by reference in C#: http://msdn.microsoft.com/en-us/library/t3c3bfhx(v=vs.80).aspx
M2H has a good converter in the asset store. If you don't have the funds to buy it they have an older free version here: http://files.m2h.nl//js_to_c.php
K
Your answer
Follow this Question
Related Questions
Enemy AI script causing Unity to crash (Javascript to C#) 1 Answer
Need help converting js to C 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Need help converting js to C# -- yield return WaitForSeconds 3 Answers