Translating from java to C#

So i’ve tried myself for a couple of hours, but i haven’t been able to make it work yet.
I wanted to translate this jave code into C#


#pragma strict

var projectile : Rigidbody;
var speed = 100;

function Update () {
if(Input.GetButtonDown(“Fire1”)){

var instantiatedProjectile : Rigidbody = Instantiate(projectile, transform.position, transform.rotation);

instantiatedProjectile.velocity = transform.TransformDirection(Vector3(0, 0, speed));

Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
}
}

What i figured out myself so far is:


using UnityEngine;
using System.Collections;

public class Weapon : MonoBehaviour {

public Rigidbody projectile;
public float speed = 100.0F;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
if(Input.GetButtonDown(“Fire1”))
Rigidbody.Instantiate(projectile, transform.position, transform.rotation);

projectile.velocity = Vector3(0, 0, speed);

Physics.IgnoreCollision(projectile.collider, transform.root.collider);
}
}

My issue seems to be the line that says
“projectile.velocity = Vector3(0, 0, speed);”

So if anyone could help me with this i’d appriciate it very much :slight_smile:

In the future please use Code tags. :smile:

projectile.velocity = new Vector3(0, 0, speed);

You must use the “new” keyword when creating an instance like this.

Will do :slight_smile: Just didn’t know how to do it ^^

This fixed it, thanks so much :slight_smile: However a new problem appeared now…

When i hit my fire button, the bullet will now collide with my weapon even though i tell it not to

Physics.IgnoreCollision(projectile.collider, transform.root.collider);

The error i get in unity says:

Any idea why this happens?

One of your colliders is either not enabled in the code, or is unchecked in the Unity editor.

Yeah, you’d think… but the weird thing is, that it works perfectly fine if i use the java version of the script…