Basically i had this working in Javascript, but im converting all my scripts to c#.
This is currently my main script so it’s important it works correctly, i think i’ve made too many changes
and i’m getting very lost.
When i currently play and press my mouse button its giving me a null reference error.
Any help would be highly appreciated.
using UnityEngine;
using System.Collections;
public class ThrowKnife : MonoBehaviour
{
public GameObject knife;
public int power;
public bool heldDown= false;
void Start ()
{
knife = Resources.Load ("Knife_Prefab") as GameObject;
}
void Update ()
{
power = 20;
if(Input.GetMouseButtonDown(0))
{
RaycastHit vHit = new RaycastHit();
Ray vRay = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(vRay, out vHit, 10000))
{
Rigidbody clone;
clone = Instantiate(knife, transform.position+Camera.main.transform.forward * -2, Quaternion.identity)as Rigidbody;
clone.transform.LookAt(vHit.point);
clone.velocity = transform.TransformDirection(Vector3.forward * power);
power = 20;
}
}
}
}