Good morning fellows members of the Unity community:
I am having a problem with CORoutines in my C# script. In my script, I wrote down this script as my coroutine method:
IEnumerator ShootMethod(float bulletspersecond)
{
yield return new WaitForSeconds(bulletspersecond);
//RayCast
print("Shot");
isShooting = false;
StopCoroutine("ShootMethod");
}
And this script I use as the Coroutine starter:
if(ShootingScript.belt[0].GetSetGameObject.active == true && isShooting == false && Input.GetMouseButton(0))
{
StartCoroutine(ShootMethod(2f));
isShooting = true;
}
But when I execute the program it doesn’t print on the console “Shot” as you can see there on the first script.
Here it is the full script (if you guys need):
public class ShootingScript : MonoBehaviour
{
bool isShooting;
public AudioSource coltShot;
public AudioSource ak47Shot;
public AudioSource m16Shot;
public AudioSource coltReload;
public AudioSource ak47Reload;
public AudioSource m16Reload;
public GameObject coltModel;
public GameObject ak47Model;
public GameObject m16Model;
static public Weapon[] belt = new Weapon[3] {new Weapon(), new Weapon(), new Weapon()};
IEnumerator ShootMethod(float bulletspersecond)
{
yield return new WaitForSeconds(bulletspersecond);
//RayCast
isShooting = false;
StopCoroutine("ShootMethod");
}
// Use this for initialization
void Start ()
{
//Setting the Weapon Specs
//Colt .45
ShootingScript.belt[0].SetGetName = "M1911";
ShootingScript.belt[0].SetShootingAudio = coltShot;
ShootingScript.belt[0].SetReloadingAudio = coltReload;
ShootingScript.belt[0].SetRetMaxAmmo = 7;
ShootingScript.belt[0].GetSetGameObject = coltModel;
ShootingScript.belt[0].GetSetSeconds = 2f;
ShootingScript.belt[0].GetSetGameObject.active = false;
//AK47
ShootingScript.belt[1].SetGetName = "Avtomat Kalashnikova 1947";
ShootingScript.belt[1].SetShootingAudio = ak47Shot;
ShootingScript.belt[1].SetReloadingAudio = ak47Reload;
ShootingScript.belt[1].SetRetMaxAmmo = 40;
ShootingScript.belt[1].GetSetGameObject = ak47Model;
ShootingScript.belt[1].GetSetSeconds = 0.1f;
ShootingScript.belt[1].GetSetGameObject.active = false;
//M16
ShootingScript.belt[2].SetGetName = "M16 Rifle";
ShootingScript.belt[2].SetShootingAudio = m16Shot;
ShootingScript.belt[2].SetReloadingAudio = m16Reload;
ShootingScript.belt[2].SetRetMaxAmmo = 60;
ShootingScript.belt[2].GetSetGameObject = m16Model;
ShootingScript.belt[2].GetSetSeconds = 0.5f;
ShootingScript.belt[2].GetSetGameObject.active = false;
ShootingScript.belt[0].SetGetHasIt = true;
ShootingScript.belt[1].SetGetHasIt = true;
ShootingScript.belt[2].SetGetHasIt = true;
}
// Update is called once per frame
void Update ()
{
//Changing weapons
if(Input.GetButton("Handgun") && ShootingScript.belt[0].SetGetHasIt == true && ShootingScript.belt[0].GetSetActivated == false)
{
ShootingScript.belt[0].GetSetActivated = true;
ShootingScript.belt[1].GetSetActivated = false;
ShootingScript.belt[2].GetSetActivated = false;
}
else if(Input.GetButton("Machinegun") && ShootingScript.belt[1].SetGetHasIt == true && ShootingScript.belt[1].GetSetActivated == false)
{
ShootingScript.belt[0].GetSetActivated = false;
ShootingScript.belt[1].GetSetActivated = true;
ShootingScript.belt[2].GetSetActivated = false;
}
else if(Input.GetButton("Submachinegun") && ShootingScript.belt[2].SetGetHasIt == true && ShootingScript.belt[2].GetSetActivated == false)
{
ShootingScript.belt[0].GetSetActivated = false;
ShootingScript.belt[1].GetSetActivated = false;
ShootingScript.belt[2].GetSetActivated = true;
}
if(ShootingScript.belt[0].GetSetActivated == true)
{
transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y, 6.8f);
ShootingScript.belt[0].GetSetGameObject.active = true;
ShootingScript.belt[1].GetSetGameObject.active = false;
ShootingScript.belt[2].GetSetGameObject.active = false;
StopAllCoroutines();
}
else if (ShootingScript.belt[1].GetSetActivated == true)
{
transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y, 10.3f);
ShootingScript.belt[0].GetSetGameObject.active = false;
ShootingScript.belt[1].GetSetGameObject.active = true;
ShootingScript.belt[2].GetSetGameObject.active = false;
StopAllCoroutines();
}
else if(ShootingScript.belt[2].GetSetActivated == true)
{
transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y, 10.3f);
ShootingScript.belt[0].GetSetGameObject.active = false;
ShootingScript.belt[1].GetSetGameObject.active = false;
ShootingScript.belt[2].GetSetGameObject.active = true;
StopAllCoroutines();
}
//Shooting Scripting itself
if(ShootingScript.belt[0].GetSetGameObject.active == true && isShooting == false && Input.GetMouseButton(0))
{
StartCoroutine(ShootMethod(2f));
isShooting = true;
}
}
}
Hope I made myself clear
Greetings
Another fellow member of the Unity Community.
Ps.: While running the script, Unity doesn’t show any errors or warnings.
PsPs.: Sorry for my bad english.