New Unity User Needs Help -Oculus Rift - Gun Shooting Reload Coroutine

Hello everyone! I just started unity with my oculus rift a few days now , im just looking for scripts around and edit or write a few lines my self so far until one day i manage to write my own scripts :smile:, I have found and edit the code below but i cant make the coroutine work, game is crashing.

Any help ?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SecondBulletShots : MonoBehaviour {

public GameObject Bullet_Emitter;
public GameObject Bullet;
public float Bullet_Forward_Force;

//SOUND
public AudioClip clip;
public AudioSource audioSource;
public Transform gunBarrelTransform;

//reloading waiting time
public float ReloadingTime;

//ammo
private int ammo = 13;

void Start ()
{
audioSource = GetComponent ();
audioSource.clip = clip;

}

void Update ()
{
if (OVRInput.GetDown (OVRInput.Button.PrimaryIndexTrigger))
{
if (ammo > 1) {
//DONISH JOYSTICK 1 DE3I CONTROLLER 0 ARISTERO
OVRHaptics.Channels[0].Preempt(new OVRHapticsClip(clip));

// HXOS
audioSource.Play ();

//The Bullet instantiation
GameObject Temporary_Bullet_Handler;
Temporary_Bullet_Handler = Instantiate(Bullet,Bullet_Emitter.transform.position,Bullet_Emitter.transform.rotation) as GameObject;

//FIX
//Temporary_Bullet_Handler.transform.Rotate(Vector3.left * 90);

//Retrieve the Rigidbody component from the instantiated Bullet and control it.
Rigidbody Temporary_RigidBody;
Temporary_RigidBody = Temporary_Bullet_Handler.GetComponent();

//Tell the bullet to be “pushed” forward by an amount set by Bullet_Forward_Force.
Temporary_RigidBody.AddForce(transform.up * Bullet_Forward_Force);

//Basic Clean Up
Destroy(Temporary_Bullet_Handler, 3.0f);

//AMMO REDUCTION
ammo–;

if (ammo == 1) {
StartCoroutine(reloads ());
}
}

}
}

IEnumerator reloads ()
{
while (true) {
if(OVRInput.GetDown(OVRInput.Button.Three))
{
yield return new WaitForSeconds (ReloadingTime);
ammo = 13;
}
}
}
}

IEnumerator reloads ()
{
while (true) {
if(OVRInput.GetDown(OVRInput.Button.Three))
{
yield return new WaitForSeconds (ReloadingTime);
ammo = 13;
}
else yield return null;
}
}
}

What was happening is you didn’t give it a frame to yield if the “if” statement was false, so it infinite looped and locked everything out.
Please read the thread at the top of this forum about using code tags.

It’s a little odd to reload at one. Perhaps the coroutine should be called before your subtract from ammo (if it’s 1) and perhaps you should be allowed to fire if ammo is >= 1?

Besides that, I do not know why you’d want your coroutine to have while(true); that would mean you’re inside that reload routine indefinitely, when I gather you want to only be there when it’s time to reload at 0 or 1 ammo.
I’m not 100% correct there, you could yield break from the routine on Button down after the reload timer - that I could understand. Another option would be to check for something like : if ammo is 0, not reloading, and Button Down → begin reloading. (in Update).

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class LeftGunShooting : MonoBehaviour {

private AudioSource sound;
public AudioClip VibeClip;

public GameObject TheBullet;
public Transform BarrelEnd;

public float despawnTime = 3.0f;
public int bulletSpeed;

public bool shootAble = true;
public float waitBeforeNextShot = 0.25f;

public int curAmmo;
public int fullAmmo = 12;
public int backupAmmo;
public float reloadTime = 3.0f;
public float curReloadTime;
public bool isReloading;

public TextMesh curAmmoText;
public TextMesh backupAmmoText;

public ParticleSystem muzzleFlash;
//public GameObject impactEffect;

public void Start()
{
curAmmo = 12;
backupAmmo = 240;
sound = GetComponent ();
}

private void Update ()
{
curAmmoText.text = curAmmo.ToString();
backupAmmoText.text = backupAmmo.ToString ();

if (OVRInput.GetDown (OVRInput.Button.PrimaryIndexTrigger) && !isReloading && curAmmo > 0)
{
if (shootAble)
{
curAmmo–;
shootAble = false;
Shoot ();
StartCoroutine (ShootingYield());
}

}

if(OVRInput.GetDown (OVRInput.Button.Three) && !isReloading && backupAmmo > 0)
{
curReloadTime = reloadTime;
isReloading = true;
Reload ();
}

if (isReloading)
{
curReloadTime -= Time.fixedDeltaTime;

if (curReloadTime <= 0)
{
isReloading = false;
}
}

}

void Reload ()
{
var shot = fullAmmo - curAmmo;

if (backupAmmo < shot)
{
curAmmo += backupAmmo;
backupAmmo = 0;
}
else
{
curAmmo += shot;
backupAmmo -= shot;
}
}

IEnumerator ShootingYield()
{
yield return new WaitForSeconds (waitBeforeNextShot);
shootAble = true;
}

void Shoot ()
{
muzzleFlash.Play ();
sound.Play ();
OVRHaptics.Channels[0].Mix(new OVRHapticsClip(VibeClip));
var bullet = Instantiate (TheBullet, BarrelEnd.position, BarrelEnd.rotation);
bullet.GetComponent ().velocity = bullet.transform.forward * bulletSpeed;

Destroy (bullet, despawnTime);
}
}

I finally manage to do it ! :smile:

I’m glad you got it solved, because I didn’t want to read it without code tags, again lol
Please look at the post about code tags for future posts on the forums :slight_smile: