Issue with switch statement

Hey guys, I seem to be having some trouble with a switch statement I have in javascript. What is supposed to happen is the character gets hit 6 times, and on the 6th time he dies, respawns if there are any lives left.

Now, the problem I am having is on the 6th hit, the life is taken away, the body parts that are supposed to be rendered are rendered, but i am in the same spot, until the character gets hit again. The 7th hit is when the character goes to the respawn point, with a hit on him. I'll post a small portion of the code, but if more is needed, just ask.

Any help would be grateful, thanks guys.

switch(HITS)
{       
    case 1:
        //disable bodypart2
        //bodyPart5.renderer.enabled = false;
        Sphere08.renderer.enabled = false;
        Sphere14.renderer.enabled = false;

    break;

    case 2:
        //disable bodypart2
        Sphere07.renderer.enabled = false;
        Sphere13.renderer.enabled = false;

    break;

    case 3:
        //disable bodypart3
        Sphere06.renderer.enabled = false;
        Sphere12.renderer.enabled = false;

    break;

    case 4:         
        //disable bodypart4
        Sphere05.renderer.enabled = false;
        Sphere11.renderer.enabled = false;

    break;

    case 5:
        //disable bodypart5
        Sphere04.renderer.enabled = false;
        Sphere10.renderer.enabled = false;

    break;

    case 6: 
        //Character is dead, respawn, lose a life, reset body parts
        CharacterMove.dead = true;
        LIVES -= 1;
        HITS = 0;

        Sphere08.renderer.enabled = true;
        Sphere14.renderer.enabled = true;

        Sphere07.renderer.enabled = true;
        Sphere13.renderer.enabled = true;   

        Sphere06.renderer.enabled = true;
        Sphere12.renderer.enabled = true;

        Sphere05.renderer.enabled = true;
        Sphere11.renderer.enabled = true;

        Sphere04.renderer.enabled = true;
        Sphere10.renderer.enabled = true;
    break;  
}

.

function LateUpdate()
{
    if (checkPoint.originalSpawn == true)
    {
        if(dead)
        {       
            transform.position = Vector3(40, 1, 17);
            transform.eulerAngles = Vector3(rotation.x, CharacterMove.rotationy, rotation.z); // rotation
            dead = false;
        }
    }   

}

.

function OnTriggerEnter( hit : Collider)
{
    if(hit.gameObject.tag == "fallout")
    {
        dead = true;
        //substract life here
        HealthControl.LIVES -= 1;
    }

    if(hit.gameObject.tag == "enemyProjectile")
    {
        //gotHit = true;
        HealthControl.HITS += 1;
        Destroy(hit.gameObject);
    }

}

@Paige: Yes I need more. Where do you call Respawn?

When do you increase HITS? Typically, this sort of thing is caused by out-of-order: do stuff with HITS; HITS++;`. Also, each of your cases could disable only the "new" stuff, to make it shorter.

So, HITS goes up on a bullet, but when does it run that big switch that uses HITS? If it isn't right after adding, there's your problem. A trick is to "play computer" and trace through everything a collision eventually does.

1 Answer

1

Well, I found the problem. For not very good reasons at all, I chose to make a separate script for handling the player running into checkpoints, and as I was transferring over all the code of that to the main character script, I saw that the respawn scripts were in the OnTriggerEnter function. so, after adding the respawn code to the Update function, all is well. I had a feeling after Owen Reynolds mentioned tracing everything the collision eventually did. Finally I can move on, heh.