Can't translate this javascript code into C#, can someone help me?

JavaScrpit:
// Public variable
public var speed : int = 6;
// Function called once when the bullet is created
function Start () {
// Get the rigidbody component
var r2d = GetComponent(“Rigidbody2D”);
// Make the bullet move upward
r2d.velocity.y = speed;
}
// Function called when the object goes out of the screen
function OnBecameInvisible() {
// Destroy the bullet Destroy(gameObject);
}
-------------------------------This is the original code i’m translating---------------------------------------------------------------
C#:
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;

public class spaceshipScript : MonoBehaviour
{
// Function called about 60 times per second
void Update()
{
// Get the rigidbody component
type r2d(GetComponentNewStruct"Rigidbody2D"));
// Move the spaceship when an arrow key is pressed
if (Input object p3 = GetKey(“right”); object p2 = r2d.velocity.x(10);
else if (Input object p = GetKey(“left”); r2d.velocity.x(-10);
else
object p1 = r2d.velocity.x(0);
}

}

[DebuggerDisplay(“{” + nameof(GetDebuggerDisplay) + “(),nq}”)]
internal struct NewStruct
{
public object Item1;
public object Item2;

public NewStruct(object item1, object item2)
{
Item1 = item1;
Item2 = item2;
}

public override bool Equals(object obj)
{
return obj is NewStruct other &&
EqualityComparer.Default.Equals(Item1, other.Item1) &&
EqualityComparer.Default.Equals(Item2, other.Item2);
}

public override int GetHashCode()
{
int hashCode = -1030903623;
hashCode = hashCode * -1521134295 + EqualityComparer.Default.GetHashCode(Item1);
hashCode = hashCode * -1521134295 + EqualityComparer.Default.GetHashCode(Item2);
return hashCode;
}

public void Deconstruct(out object item1, out object item2)
{
item1 = Item1;
item2 = Item2;
}

public static implicit operator (object, object)(NewStruct value)
{
return (value.Item1, value.Item2);
}

public static implicit operator NewStruct((object, object) value)
{
return new NewStruct(value.Item1, value.Item2);
}

private string GetDebuggerDisplay()
{
return ToString();
}
}

This is what i’ve got until now, i still think it isn’t neir close.

difficult to see the code… can you add these code tags

try this tutorial also:

This original Javascript code is friggin’ NOTHING… it gets a Rigidbody2D reference, then sets it Y velocity equal to speed… this is NOT worth translating because it literally will get you nowhere at all.

Whatever it is you are starting to try to do, start from a modern tutorial, don’t waste your time on Javascript. It’s been deprecated in Unity for five years and unavailable for two at least.

PS: all the sheets of C# conversion code in the second half of your post is language / class boilerplate crap that you absolutely do not need to do what the original Javascript does: get an RB2D, set its Y velocity, probably to 6.

3 Likes