Advanced VR Mechanics With Unity and the HTC Vive – Part 2 (Need Help)

Hey guys, I’m going through the Advanced VR Mechanics With Unity Part 2 and I’ve come across a problem.

I know this post is old, but things seemed to work so smoothly so I’m sure it’s a problem with my code and not just that the tutorial is outdated.

Basically I’ve completed the tutorial up until just before creating the backpack.

My bow can fire the arrow that is inside it perfectly fine, the arrow fires and gets stuck as intended and then it’s no longer visible in the bow as intended :).

My problem is, that once I add a new arrow to the bow, it attached to the bow just fine like I expect it to. But the moment I start to pull the arrow back. My controller vibrates indefinitely and releasing the trigger doesn’t shoot the arrow. It’s weird because it works for the first arrow, but not for the second.

Does anyone know where I should look to sort out this issue?

@blackdragonbe Can you please help with this when you get a chance? Thank you - much appreciated! :]

1 Like

Additional Information:

I think I’ve scoped it down to the following… I noticed this error in the console:

MissingReferenceException: The object of type ‘GameObject’ has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, Vector3 pos, Quaternion rot) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineObjectBindings.gen.cs:52)
UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:156)
UnityEngine.Object.Instantiate[GameObject] (UnityEngine.GameObject original, Vector3 position, Quaternion rotation) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:206)
Bow.ShootArrow () (at Assets/Scripts/Bow.cs:65)
RWVR_ArrowInBow.OnTriggerWasReleased (.RWVR_InteractionController controller) (at Assets/Scripts/RWVR/RWVR_ArrowInBow.cs:35)
RWVR_InteractionController.Update () (at Assets/Scripts/RWVR/RWVR_InteractionController.cs:79)

Here’s the relevant code for that.

// Accept a Collider as a parameter. This is the trigger that hit the bow.
    private void OnTriggerEnter(Collider other)
        // This long if-statement returns true if the bow is unarmed and is hit by a RealArrow.
        // There's a few checks to make sure it only reacts to arrows that are held by the player.
        if (
                && other.CompareTag("InteractionObject")
                && other.GetComponent<RealArrow>()
                && !other.GetComponent<RWVR_InteractionObject>().IsFree()
        ) {
            Destroy(other.gameObject); // Destroy the RealArrow.
            Arm(); // Arm the bow.

    public void ShootArrow()
        // Spawn a new RealArrow based on its prefab. Set its position and rotation equal to that of the bow.
        GameObject arrow = Instantiate(realArrowPrefab, transform.position, transform.rotation);
        // Calculate the distance between the bow and the attached arrow and store it in distance.
        float distance = Vector3.Distance(transform.position, attachedArrow.position);

        // Give the RealArrow a forwared velocity based on distance. The further the string gets pulled back, the more velocity the arrow will have.
        arrow.GetComponent<Rigidbody>().velocity = arrow.transform.forward * distance * maxShootSpeed;
        AudioSource.PlayClipAtPoint(fireSound, transform.position); // Play the bow firing sound.
        GetComponent<RWVR_InteractionObject>().currentController.Vibrate(3500); // Vibrate the controller to give tactile feedback.
        arrow.GetComponent<RealArrow>().Launch(); // Call the RealArrow's Launch() method.

        Disarm(); // Disarm the bow.

It says it can’t Instantiate the arrow GameObject because it was destroyed. But isn’t this a new object that’s being created? I don’t understand why this doesn’t work…


Hey there!

Have you compared your code with the one in the final project?
It’s been a while since I wrote the code, but it sounds like a value isn’t reset after the first arrow is shot.

Can you take a look at the ArrowInBow code? My guess would be that this is missing (line marked with THIS LINE:

public override void OnTriggerWasReleased(RWVR_InteractionController controller) // 1
    attachedBow.GetComponent<Bow>().ShootArrow(); // 2
    currentController.Vibrate(3500); // 3
    base.OnTriggerWasReleased(controller); // 4 // <<<<<< THIS LINE


1 Like

Hey thanks for getting back to me :slight_smile:

I just found out where I screwed up when I continued with the backpack section.

It turns out that in the bow script configuaration, I set the Real Arrow Prefab to the RealArrow GameObject instead of the RealArrow Prefab.

Thanks so much for this tutorial. I really learnt a lot from it. I’ve got lots of programming experience but had none with Unity and VR development before doing this. Looking forward to anymore stuff you might come up with in the future.

All the best!

Hey Barry,

I’m glad you found a solution!
Thanks for the kind words, I appreciate that.


1 Like

This topic was automatically closed after 166 days. New replies are no longer allowed.