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.
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…