0

I want to make an example of shot, then I wrote this in the handle button event,

using UnityEngine;
using System.Collections;

public class fire : MonoBehaviour {
public GameObject bullet;

SteamVR_TrackedObject trackedObj;
void start() {
    trackedObj = GetComponent<SteamVR_TrakedObject>();
}
void Update() {
    var device = SteamVR_Controller.Input((int)trackedObj.index);
    if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger)) {
        GameObejct obj = Instantiate(bullet,transform.position);
        Vector3d fwd = transform.TransformDirection(Vector3.forward);
        obj.GetComponent.<Rigidbody>().AddForce(fwd*2800);
    }
}
}

but when debugging and I press handle button ,it didn't produce a bullet,and had erred at the line
var device = SteamVR_Controller.Input((int)trackedObj.index);, the error is:

Object reference not set to an instance of an object.

Abhishek Aryan
  • 19,936
  • 8
  • 46
  • 65
project_01
  • 31
  • 4

1 Answers1

0

First You should need to confirm that your fire script is attached to your controller object and your controller object also attached SteamVR_TrackedObject script (which provide by steam plugin). Then, lastly ensure this line is executing

void start() {
    trackedObj = GetComponent<SteamVR_TrakedObject>();
}
Muhammad Faizan Khan
  • 10,013
  • 18
  • 97
  • 186