Some Help here, please!!!

Guys…

I have a little problem that is making me crazy…

I have a Spaceship in my prefabs, and it has checked, the Sprite Renderer, Animator, Rigidbody2d, Box Collider2d (with trigger), another Box Collider 2D (without trigger but with material), and 2 scripts, one that handles animations, and the other one that handles the whole spaceship…

In this last script I have the OnTriggerEnter2D saying that when the SP gets hit by an enemy bullet, it is destroyed, respawned and invulnerable for 3 secs.

Now, my problem is that the “new SP” enters into the screen with nothing but the sprite renderer checked.

Does anyone know why this is happening?

Here is the whole code

using UnityEngine;
using System.Collections;

public class SPControl : MonoBehaviour
{

    private float leftBorder;
    private float rightBorder;
    private float upperBorder;
    public float downBorder;

    public float moveX;
    public float moveY;
    public float moveSpeed = 5;

    public GameObject SL;

    public GameObject bullet;
    public int Lives;
    public int life = -1;

    public bool invulnerability;
    public bool losingLife;

    public float _actualTime;
    public float _triggerTime;


    public GameObject explosion;
    public GameObject _player;
    private Vector3 lastPlayerPosition;

    void Start()
    {       
        var leftVector = new Vector3(0, 0, (transform.position - Camera.main.transform.position).z);
        var rightVector = new Vector3(1, 0, (transform.position - Camera.main.transform.position).z);
        var upVector = new Vector3(1, 1, (transform.position - Camera.main.transform.position).z);
        var downVector = new Vector3(0, 1, (transform.position - Camera.main.transform.position).z);
        leftBorder = Camera.main.ViewportToWorldPoint(leftVector).x;
        rightBorder = Camera.main.ViewportToWorldPoint(rightVector).x;
        upperBorder = Camera.main.ViewportToWorldPoint(upVector).x;
        downBorder = Camera.main.ViewportToWorldPoint(downVector).x;
        Debug.Log(leftBorder);
        Instantiate(SL, new Vector3(leftBorder, (downBorder), (transform.position - Camera.main.transform.position).z), Quaternion.identity);
        Instantiate(SL, new Vector3(rightBorder, (downBorder), (transform.position - Camera.main.transform.position).z), Quaternion.identity);

        _actualTime = 0;
        _triggerTime = 3;
        invulnerability = false;
        losingLife = false;
        lastPlayerPosition = _player.transform.position;


    }

    void Update()
    {
        _actualTime += Time.deltaTime;
       
        if (Input.GetButtonDown("Fire1"))
        {
            Instantiate(bullet, transform.position, transform.rotation);
        }

        if (Input.GetButton("Horizontal"))
        {

            if (transform.localPosition.x < leftBorder + 1f)
            {
                moveX = 0.018f;
            }
            else if (transform.localPosition.x > rightBorder - 1f)
            {
                moveX = -0.018f;

            }
            else
            {
                moveX = (moveSpeed * Time.deltaTime * Input.GetAxis("Horizontal"));
            }
        }
                   else
                   {
                      
        transform.Translate(moveX, moveY, 0);
    }

    public void Explosion()
    {
        Instantiate(explosion, transform.position, transform.rotation);
    }

    void OnTriggerEnter2D(Collider2D c)
    {
        string layerName = LayerMask.LayerToName(c.gameObject.layer);

        if (layerName == "Bullet (Enemy)")
        {

            Destroy(c.gameObject);
        }

        if (layerName == "Bullet (Enemy)" || layerName == "Enemy")
        {
            if (invulnerability == false)
            {
                Explosion();

                Destroy(gameObject);

                if (Lives >= 1)
                {
                    Lives--;
                    losingLife = true;
                    _actualTime = 0;
                    LosingLife();
                }
                else
                {
                    print("Game OVer");
                }
            }
        }
    }

    void LosingLife()
    {
        if (losingLife == true)
        {
            GameObject P = new GameObject();
            P = Instantiate(_player, new Vector3((lastPlayerPosition.x), (lastPlayerPosition.y), 0), Quaternion.identity)as GameObject;
            losingLife = false;
            invulnerability = true;
        }
        if (invulnerability == true)
        {
            if (_actualTime >= _triggerTime)
            {
                invulnerability = false;
            }
        }
    }
}

What you dragged on _player: a scene object or a prefab?

a prefab… actually I created an empty GO, when the game starts, and I instantiate the same prefab from there, with a diferrent script (only instantiating it) and it works fine… but, when it has to be respawned, it comes in with all the components unchecked

I am not sure (it’s a bit later…) but perhaps you have to Destroy(_player) instead of gameObject. And also set _player = Instantiate(_player …

Anyway why Destroying if you want to reuse?

Edit: Perhaps you have to Instantiate before Destroying

1 Like