- Home /
Can someone translate C# to Javascript?
Can someone please translate this into Javascript:
if (GameObject.Find("cube").GetComponent<bonus>().bonuson)
Cube is the Object with the Script bonus.js and this Script needs to check if bonuson is true.
Thanks.
$$anonymous$$y script is still not working. :(
I have a Player with the Script slo$$anonymous$$o and a cube with the Script bonus. And if the Player gets the cube with the Script bonus, he can slow down time by pressing the button "slomo".
So how can I make the Script slo$$anonymous$$o check if the Player got the cube?
The Script on the Player (slo$$anonymous$$o):
var currentBulletTimer : float = 0;
var bulletTimeAllowed : float = 3.0; // seconds
function Update () {
if (GameObject.Find("cube").GetComponent(bonus).jep)
if (Input.GetButtonDown ("slomo")) {
if (Time.timeScale == 1.0)
Time.timeScale = 0.3;
else
Time.timeScale = 1.0;
//Time.fixedDeltaTime = 0.02 * Time.timeScale;
}
if (Time.timeScale == 0.3) {
currentBulletTimer += Time.deltaTime;
}
if ( currentBulletTimer > bulletTimeAllowed ) {
currentBulletTimer = 0;
Time.timeScale = 1.0;
}
}
The Script on cube (bonus)
var jep : boolean = false;
function OnTriggerEnter(other : Collider)
{
if (other.gameObject.tag == "Player")
jep = true;
Destroy(gameObject);
}
Thanks for helping.
This is really a new question. You have a problem here. In your bonus script, you set 'jep = true', but then you immediately destroy the cube and therefore the script that contains 'jep' is also destroyed. That means that your GameObject.Find() in slo$$anonymous$$o will not find this specific cube. You have a many choices and here are a few.
First, you could not destroy the cube, ins$$anonymous$$d just hide it. Replace line 7 in the 'bonus' script with:
renderer.enabled = false;
collider.enabled = false;
Another solution would be for the cube to set a variable in the 'slo$$anonymous$$o' script that says he got the cube. At the top of the 'slo$$anonymous$$o' script have:
var gotCube = false;
Then in the cube code:
if (other.gameObject.tag == "Player") {
sm = other.gameObject.GetComponent(slo$$anonymous$$o);
sm.gotCube = true;
}
Another solution (probably the best solution) if your player does not have a character controller, is to have an OnTriggerEnter() on the player, and set the 'gotCube' variable inside that function.
A couple of notes:
Line 7 of 'bonus' gets execute regardless of whether the tag is 'Player'. So if something else besides the player hits the cube, it will be Destoryed.
Line 5 of slo$$anonymous$$o find a instance of an object named 'cube'. If you have multiple 'cube' objects, it will not necessarily find the one that had the trigger fire.
Answer by robertbu · Feb 18, 2014 at 04:17 PM
Almost the same:
if (GameObject.Find("cube").GetComponent(bonus).bonuson)
http://answers.unity3d.com/questions/12911/what-are-the-syntax-differences-in-c-and-javascrip.html
Answer by ragnaros100 · Feb 19, 2014 at 07:28 AM
In general it isnt the usual code of cunduct to post "Please do this for me"-questions around here. I can see that you are new, but please excercise more caution in the future.