Hey guys I was wondering if any of you coding gurus could help me convert the following Javascript code into a C# script. I’m not a programmer by any means and am having some trouble trying to figure it out! Thanks!
var prefab : GameObject;
var PrefabCount = 300;
var minXPos = -118;
var maxXPos = 109;
var minZPos = 10;
var maxZPos = -10;
var minScale = 0.75;
var maxScale = 1.5;
function Start () {
for (var count = 0; count < PrefabCount; count++) {
var position = Vector3
(Random.Range(-118, 109), 0, Random.Range(10, -10));
var hit : RaycastHit;
if (Physics.Raycast (position, -Vector3.up, hit)) {
distanceToGround = hit.distance;
position.y = position.y - distanceToGround;
}
var tree = Instantiate(prefab, position, Quaternion.FromToRotation(Vector3.forward, Vector3.up));
var scale = Random.Range(minScale, maxScale);
tree.transform.localScale = Vector3(scale, 1.0, scale);
}
}
I’ll you a few pointers, then you can try and convert it on your own. You’ll learn a lot better that way, and learn how to read the error messages in the console.
Variables in JS look like this:
var prefab : GameObject;
or
var position = Vector3 (0,0,0);
Variables in C# look like this:
public GameObject prefab;
or
private Vector3 position = new Vector3 (0,0,0);
Public variables can be accessed from other scripts and are displayed in the Inspector. Private variables can only be used within the current script and are not displayed in the inspector.
Functions in JS look like this:
function Start () {
}
Functions in C# look like this:
void Start () {
}
void means it has no return value. If the function does return something replace “void” with the data type of the object your returning.
Create a new C# script in Unity it will look like this:
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
“NewBehaviourScript” must be changed to match the filename of your script (without the extension).
There’s more to it then that but this is all I have time to write for now…It should be enough to convert this script…I didn’t re-read what I just wrote so hopefully I wrote everything correctly and you understand everything…because I didn’t explain any logic.
Start trying to convert it, if you have any problems post what you have and any errors in the console and we will help you fix it and explain why.
Hey thank you very much for your help! I really appreciate it! This should definitely get me started!
If anyone is curious, here is the C# version of the above script:
// TreeGenerator.cs
// This script instantiates prefabs onto a mesh in random positions and sizes.
using UnityEngine;
using System.Collections;
public class TreeGenerator : MonoBehaviour {
public GameObject prefab;
public int TreeCount = 300;
public float minXPos = -118.0f;
public float maxXPos = 109.0f;
public float minZPos = 10.0f;
public float maxZPos = -10.0f;
public float minScale = 0.75f;
public float maxScale = 1.5f;
// Initialization
void Start () {
for (int count = 0; count < TreeCount; count++) {
Vector3 position = new Vector3
(Random.Range(minXPos, maxXPos), 0, Random.Range(minZPos, maxZPos));
RaycastHit hit = new RaycastHit();
if (Physics.Raycast (position, -Vector3.up, out hit)) {
position.y -= hit.distance;
}
Instantiate(prefab, position, Quaternion.FromToRotation(Vector3.forward, Vector3.up));
float scale = Random.Range(minScale, maxScale);
prefab.transform.localScale = new Vector3(scale, 1.0f, scale);
}
}
hey, with this code, how can you declare the hit.point as rayCastPoint? I mean, in javascript, I used it as :
var rayCastPoint : RayCastHit;
then inside the function,
rayCastPoint = hit.point;
How is that in c#? :? [/code]
just put “public RaycastHit raycastPoint;” in that list of class variables in the class definition. You can then access “raycastPoint” in any class function.