Trying to convert my old js script to C#
using UnityEngine;
using System.Collections;
public class Xlauncher : MonoBehaviour {
public Transform rocketPrefab;
public Transform rocket2Prefab;
public float rocketSpeed = 20;
public float rocket2Speed = 15;
public Transform spawnPoint;
public Texture2D crosshairTexture;
public Rect position;
//------------------------------------------------------------------------------
private float Attack1nextShoot = 0;
private float Attack2nextShoot = 0;
public float Attack1fireRate = 1;
public float Attack2fireRate = 1;
//-------------------------------------------------------------------------------
public Vector2 pos = new Vector2(20,40);
public Vector2 size = new Vector2(60,20);
public float time;
void Update () {
position = new Rect((Screen.width - crosshairTexture.width) / 2, (Screen.height - crosshairTexture.height) /2, crosshairTexture.width, crosshairTexture.height);
Ray ray = Camera.main.ViewportPointToRay( new Vector3(0.5f,0.5f,0));
RaycastHit hit = new RaycastHit();
Vector3 dir;
if (Input.GetButton("Fire1") Time.time > Attack1nextShoot){
Attack1nextShoot = Time.time + Attack1fireRate;
if (Physics.Raycast(ray, out hit)){
dir = (hit.point - spawnPoint.position).normalized;
}
else {
dir = ray.direction;
}
Quaternion rot = Quaternion.FromToRotation(rocketPrefab.forward, dir);
Rigidbody rocket = Instantiate(rocketPrefab, spawnPoint.position, rot) as Rigidbody;
rocket.rigidbody.velocity = dir * rocketSpeed;
}
else if (Input.GetButtonUp("Fire2") Time.time > Attack2nextShoot){
Attack2nextShoot = Time.time + Attack2fireRate;
if (Physics.Raycast(ray, out hit)){
dir = (hit.point - spawnPoint.position).normalized;
}
else {
dir = ray.direction;
}
Quaternion rot2 = Quaternion.FromToRotation(rocket2Prefab.forward, dir);
Rigidbody rocket2 = Instantiate(rocket2Prefab, spawnPoint.position, rot2) as Rigidbody;
rocket2.rigidbody.velocity = dir * rocket2Speed;
}
}
void OnGUI()
{
GUI.DrawTexture(position, crosshairTexture);
}
}
And here is an error I get when trying to fire
NullReferenceException: Object reference not set to an instance of an object
Xlauncher.Update () (at Assets/Scripts/Weapon/Xlauncher.cs:53)