- Home /
Converting JS to C# Issue
I'm trying to convert a prefab shoot script from JS to C# and am getting errors I don't know how to solve:
// Converted from UnityScript to C# at http://www.M2H.nl/files/js_to_c.php - by Mike Hergaarden
// Do test the code! You usually need to change a few small bits.
using UnityEngine;
using System.Collections;
public class PrefabShooting : MonoBehaviour {
Vector3 torque;
Rigidbody theBullet;
int Speed = 20;//<- FIXME_VAR_TYPE
int rndMag = 200;//<- FIXME_VAR_TYPE
void Update (){
if (Input.GetMouseButtonDown(0)){
var clone = Instantiate(theBullet, transform.position, transform.rotation);//<- FIXME_VAR_TYPE
Vector3 camDir = Camera.main.transform.forward;//<- FIXME_VAR_TYPE
clone.velocity = transform.TransformDirection(Vector3(0, 0, Speed));
// ADD RANDOM TORQUE
torque.x = Random.Range (rndMag*-1, rndMag);
torque.y = Random.Range (rndMag*-1, rndMag);
torque.z = Random.Range (rndMag*-1, rndMag);
clone.AddTorque(torque);
Destroy (clone.gameObject, 30);
}
}
}
The FIXME_VAR_TYPE commented lines are where I just came up with my best guess for variable types. I saw someone mentioning using "Debug.Log(myVariable.GetType() );" in another post, but I don't understand how I'm supposed to use that if the variable creates a compile error and prevents you from running the script in the first place.
Provide console output and more information about your error, please.
Answer by DiegoSLTS · Jul 14, 2015 at 12:36 AM
Speed should be a float if you'll use it to create a Vector3.
rndMag should be a float too, unless you actually want to use the Random.Range function that returns an int value (doesn't look like that if you assign it to the components of a Vector3).
"clone" could be a Rigidbody since you're instantiating a new object from "theBullet", which is a RigidBody. You can cast the value returned by instantiate or you can use the generics version:
Rigidbody clone = Instantiate<Rigidbody>(theBullet, transform.position, transform.rotation);
camDir is a Vector3, that's OK.
And this is not a type issue, it's not even an issue, but instead of:
Random.Range (rndMag*-1, rndMag)
it would be easier to read if you write:
Random.Range (-rndMag, rndMag);
Answer by maccabbe · Jul 14, 2015 at 12:28 AM
1 - avoid using variables of type 'var' (line 16)
2 - declare new objects using new keyword (line 18)
The following compiles but you should deal with the warnings. As the warnings should indicate, you never assign anything to theBullet and it can't be assigned from the outside as it is not public (line 10). Also camDir is assigned but never used (line 17).
using UnityEngine;
using System.Collections;
public class PrefabShooting : MonoBehaviour {
Vector3 torque;
Rigidbody theBullet;
int Speed = 20;//<- FIXME_VAR_TYPE
int rndMag = 200;//<- FIXME_VAR_TYPE
void Update (){
if (Input.GetMouseButtonDown(0)){
Rigidbody clone = Instantiate(theBullet, transform.position, transform.rotation) as Rigidbody;//<- FIXME_VAR_TYPE
Vector3 camDir = Camera.main.transform.forward;//<- FIXME_VAR_TYPE
clone.velocity = transform.TransformDirection(new Vector3(0, 0, Speed));
// ADD RANDOM TORQUE
torque.x = Random.Range (rndMag*-1, rndMag);
torque.y = Random.Range (rndMag*-1, rndMag);
torque.z = Random.Range (rndMag*-1, rndMag);
clone.AddTorque(torque);
Destroy (clone.gameObject, 30);
}
}
}