Player suddenly disappears!

Hi to everyone. I apologize since now for my english. I’m making a 2D arcade shoot-em-'up game, in which you control a spaceship that has to destroy other ones. On a day, a problem has appeared in my project: The spaceship will be suddenly destroied during game execution, disappearing from the scene (I’m saying “destroied” because, if I pause the game and return to the scene view, the spaceship is no more on the scene - and that’s definitely not a rendering problem).
I noticed two things:

  1. My Unity, during the period in which the problem appeared, crashed. As a consequence, “Player” prefab, that’s the spaceship, lost its “box”-icon, and I had to re-imported it. After that, however, the icon reappeared, and that’s no track of damaged data.

  2. The spaceship (generally) disappears when an object never seen before is rendered on the scene (example: an enemy type appearing in the game for the first time). The game has a little framerate loss (I’m speaking about an half second duration) and, when the framerate turns back to normal, the spaceship is no more on the scene.
    If i replay the game, the situation seems going better, with the ship that remains on the scene, until the next “new” object rendered (but, I repeat, it’s possible that the player disappears also during a “calm” situation, even with a lower probability)

Nobody encountered a similar problem before? Thanks to all for the reading, I apoligize for the topic lenght and I hope to have reported sufficient informations.

        Andrea

Don't apologize for your english! You are more polite that 90% of the internet. Reading your question just made my day!

Hey! I've really appreciated that! Thank you ;)

4 Answers

4

Sorry for bumping this after months, but I’ve finally found the error that I’ve done!

Well…I’m feeling like a total noob…I simply attached a TrailRenderer to my spaceship, and this had the Autodestruct parameter activated (I misunderstood the real meaning of that parameter, when I’ve began this project…)

Thanks again at who partecipated in this thread (that, maybe, could have been interested in my stupid error)!

Take a look at the Hierarchy panel: if the player gets destroyed, it disappears from the Hierarchy list. Sometimes we think some object disappeared, but it has just changed position due to some bug in our scripts. Click the player before run, and it will appear in the Inspector during all the game - this way you can see its Position field and know if something changes the player position.

Thank you very much, Aldo. I've followed your advice and the debug prints the first message. So, the spaceship is destroied by "something", but I can't figure out what it is. I'll do other experiments in according with the other objects on the scene (the project has became a bit large, indeed...Other debug has to be done somewhere!) and, if I will discover the cause, I'll let you know. Your help since now has been very appreciated.

Try to find the word "Destroy" in all scripts in your project (use Windows Search or the Mac equivalent). Pay attention specially to OnCollision/OnTrigger events: a commom mistake is to do something like "Destroy(gameObject)" instead of "Destroy(other.gameObject)"

Using Tags like: |Enemy|Champion|GoldBoost| is likely a better way... Then you can just use: if (tag.Contains("|Champion|")) { MakeBadAss(); }, and you know you'll never accidentally get back a substring of another Tag (if you end up with hundreds [or thousands] of them)...

Thank you for the answer. However, I’ve already tried what you said (I was not precise in my description last time, sorry): I pause the game, turn back to Hierarchy panel, and “Player” is no more in the list (letting me deduce that’s destroied).

I report the script attached to the Player, maybe they could help in understanding the problem:

  1. This script let the spaceship shooting, in according to its firepower level:
    var newObject : Transform;

    function Update () {
    firepower= GetComponent(KillPlayer).firepower;
    var pos : Vector3 = transform.position;
    if (Input.GetButtonDown("Jump") && firepower==1) {
        Instantiate(newObject, Vector3(pos.x-4,pos.y,pos.z),transform.rotation);
    }else if (Input.GetButtonDown("Jump") && firepower==2) {								
		Instantiate(newObject, Vector3(pos.x-4,pos.y,pos.z-1), transform.rotation);
		Instantiate(newObject, Vector3(pos.x-4,pos.y,pos.z+1), transform.rotation);
	}else if (Input.GetButtonDown("Jump") && firepower==3) {
		Instantiate(newObject, Vector3(pos.x-4,pos.y,pos.z-1), transform.rotation);
		Instantiate(newObject, Vector3(pos.x-4,pos.y,pos.z), transform.rotation);
		Instantiate(newObject, Vector3(pos.x-4,pos.y,pos.z+1), transform.rotation);
	}else if (Input.GetButtonDown("Jump") && firepower>=4) {
		Instantiate(newObject, Vector3(pos.x-4,pos.y,pos.z-1), transform.rotation);
		Instantiate(newObject, Vector3(pos.x-4,pos.y,pos.z), transform.rotation);
		Instantiate(newObject, Vector3(pos.x-4,pos.y,pos.z+1), transform.rotation);
		Instantiate(newObject, Vector3(pos.x-10,pos.y,pos.z-0.5), transform.rotation);
		Instantiate(newObject, Vector3(pos.x-10,pos.y,pos.z+0.5), transform.rotation);
	}}

2)This one let the ship move vertically during time (because it’s an arcade) and avoids that ship exits from the screen (only from left and right, for now):

    var speed=5.0;

function Update () {

	var y=Time.deltaTime*speed;
	transform.Translate(0,y,0);
	
	var pos : Vector3 = transform.position;
	if (pos.z>27)			//avoid hiding outside the FOV
		transform.Translate(-1, 0, 0);
	else if (pos.z<-27)
		transform.Translate(1, 0, 0);
}

  1. This make the ship controllable by arrow keys:
    var speed = 20.0;

function Update () {

var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
var y = Input.GetAxis("Vertical") * Time.deltaTime * speed;
transform.Translate(x, y, 0);

}

  1. And, at last, the control about player’s distruction (no respawn for now) - (“Finish” tag is obviosly attached to enemies):
var explosionEffect : Transform;

function OnCollisionEnter (theCollision : Collision) {

   if (theCollision.gameObject.tag=="Finish"){
       Instantiate(explosionEffect, transform.position, transform.rotation);
       Destroy(gameObject);
   }
}

It seems to me that anything in the scripts could create such problem (moreover, the only destruction allowed by last script is in conjunction with an explosion, so I also excluded a “not-considered-case” about it, since the explosion doesn’t take place when the ship disappears).

Thank you in advice, hope that someone could have other ideas.

I think I may know the problem happened to me. Does the spaceship shoot? Does the spaceship have a life bar attached to it? Cause sometimes what happens is that you shoot your self. So move the Empty Gameobject or what every is shooting a bit away from the spaceship.

Thanks a lot, jimmyismike, but, certainly, that's not the problem, because I've just considered that case: in the first script, I've generated the projectiles at pos.x-4: That "-4" avoid the ship to kill herself.