Camo001
Camo001

Reputation: 49

When I press play a gameobject disappears

I am creating a FPS game in Unity and have a gun attached to the firstpersoncamera from the Standard Assets, that worked fine until now, when I press the Play button in the editor the gun disappears.

I followed all the steps here and it still disappears.

I'm very new to making games with Unity so I probably missed something obvious.

This is the code:

    using UnityEngine;
    using System.Collections;

    public class PlayerShooting : MonoBehaviour {

    public ParticleSystem muzzleFlash;
    Animator anim;
    public GameObject impactPrefab;

    GameObject[] impacts;
    int currentImpact = 0;
    int maxImpacts = 5;

    bool shooting = false;

    // Use this for initialization
    void Start () {

        impacts = new GameObject[maxImpacts];
        for(int i = 0; i < maxImpacts; i++)
            impacts[i] = (GameObject)Instantiate(impactPrefab);

        anim = GetComponentInChildren<Animator> ();
    }

    // Update is called once per frame
    void Update () {

        if(Input.GetButtonDown ("Fire1") && !Input.GetKey(KeyCode.LeftShift))
        {
            muzzleFlash.Play();
            anim.SetTrigger("Fire");
            shooting = true;
        }

    }

    void FixedUpdate()
    {
        if(shooting)
        {
            shooting = false;

            RaycastHit hit;

            if(Physics.Raycast(transform.position, transform.forward, out hit, 50f))
            {
                if(hit.transform.tag == "Enemy")
                    Destroy (hit.transform.gameObject);

                impacts[currentImpact].transform.position = hit.point;
                impacts[currentImpact].GetComponent<ParticleSystem>().Play();

                if(++currentImpact >= maxImpacts)
                    currentImpact = 0;
            }
        }
    }
}

Upvotes: 0

Views: 6897

Answers (2)

Camo001
Camo001

Reputation: 49

Solved it, changed the Depth of the GunCamera to 1 instead of 0 and now it works.

Upvotes: 1

Jerin
Jerin

Reputation: 4239

enter image description hereGameObject via GameObject->Create Empty and name it Now this should be attached as child of the Main Camera Now this Main Camera isnt the deault main camera but the main camera present inside your First person Controller object that you added.

Any screenshot or sample code would have helped in finding the exact error

Upvotes: 0

Related Questions