Sign up ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am having a bit of an issue with getting an empty bullet shell to clone every time a bullet is shot. The first empty shell appears and drops, but after the first empty shell, nothing is cloned.

I am struggling to figure out what the issue is. I am trying it on the first initial weapon before trying other weapons. My code is as follows:

    using UnityEngine;
using System.Collections;
using System;

public class RecoilScript : MonoBehaviour {
    public GameObject PistolRecoil;
    public GameObject RifleRecoil;
    public GameObject ShotgunRecoil;

    // Use this for initialization
    void Start () {
        PistolRecoil.SetActive(false);
        RifleRecoil.SetActive(false);
        ShotgunRecoil.SetActive(false);
    }

    // Update is called once per frame
    void Update () {
        if (Input.GetKey(KeyCode.LeftControl) && SoldierController.canFire == true)
        {
            if (SoldierController.weaponType == 1)
            { 
                PistolRecoil.SetActive(true);
                RifleRecoil.SetActive(false);
                ShotgunRecoil.SetActive(false);
                StartCoroutine(recoilClone());
            }
            else if (SoldierController.weaponType == 2)
            {
                PistolRecoil.SetActive(false);
                RifleRecoil.SetActive(true);
                ShotgunRecoil.SetActive(false);
            }
            else if (SoldierController.weaponType == 3)
            {
                PistolRecoil.SetActive(false);
                RifleRecoil.SetActive(false);
                ShotgunRecoil.SetActive(false);
            }
            else if (SoldierController.weaponType == 4)
            {
                PistolRecoil.SetActive(false);
                RifleRecoil.SetActive(false);
                ShotgunRecoil.SetActive(true);
            }
        }

    }

     IEnumerator recoilClone()
    {
        Instantiate(PistolRecoil, SoldierController.positionGun, transform.root.rotation);
        yield return new WaitForSeconds(0.5f);
        Destroy(PistolRecoil);

        //throw new NotImplementedException();
    }
}
share|improve this question
    
Your coroutine throws an exception. Get rid of that. – PatrickSharbaugh Dec 2 '15 at 22:07
    
@PatrickSharbaugh I have updated my code, but unfortunately same issue :( – Mubeen Hussain Dec 2 '15 at 22:10

In these lines:

    Instantiate(PistolRecoil, SoldierController.positionGun, transform.root.rotation);
    yield return new WaitForSeconds(0.5f);
    Destroy(PistolRecoil);

you are not destroying the clone, you are destroying the original. Similarily, there is some code further above where you are manipulating the original, although you likely mean to manipulate the clone(s).

It appears you use the technique of creating a prefab, removing it from the scene, adding the prefab as a script variable of another game object in the scene and then using that as a template for instantiating clones. In that case your script usually does not meddle with the template at all. If you want the script to interact with the cloned objects, save the return value of Instantiate in a separate variable:

GameObject recoil = Instantiate(PistolRecoil, SoldierController.positionGun, transform.root.rotation) as GameObject;
yield return new WaitForSeconds(0.5f);
Destroy(recoil);

Oh, and a minor style advise: The disappearing is a part of the behavior of the casing not of the soldier, so I would rather implement it as a separate script attached to the PistolRecoil game object.

share|improve this answer
1  
Looks like the same root cause as in this issue: after instantiating a copy of some SourceObject, assuming that manipulating the variable holding SourceObject will manipulate the new copy, instead of capturing the reference to the copy returned from Instantiate() and manipulating that. This seems to trip up a lot of people, but it's hard to search for the issue unless you already understand the cause. – DMGregory Dec 2 '15 at 23:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.