script, losing lives

I have made a script (my first one on my own). The meaning is that if you got hit there is gonna ba a text above your head like : -2 or -1. But when a run my game and I got hit. He keeps saying: NullReferenceExeption. Can somebody help me? This is the script:

var lose1livePrefab : Transform;

var lose2livesPrefab : Transform;

static var LOSELIVES = 6;

function Update ()

{   
     switch(LOSELIVES)

        {
            case 6 -1:
                    var lose1live1 = Instantiate(lose1livePrefab, transform.Find("Worm.SpawnPoint").transform.position, Quaternion.identity);
                break;

            case 6 -2:
                    var lose2lives1 = Instantiate(lose2livesPrefab, transform.Find("Worm.SpawnPoint").transform.position, Quaternion.identity);
                break;

            case 5 -1:
                    var lose1live2 = Instantiate(lose1livePrefab, transform.Find("Worm.SpawnPoint").transform.position, Quaternion.identity);
                break;

            case 5 -2:
                    var lose2lives2 = Instantiate(lose2livesPrefab, transform.Find("Worm.SpawnPoint").transform.position, Quaternion.identity);
                break;

            case 4 -1:
                    var lose1live3 = Instantiate(lose1livePrefab, transform.Find("Worm.SpawnPoint").transform.position, Quaternion.identity);               
                break;

            case 4 -2:
                    var lose2lives3 = Instantiate(lose2livesPrefab, transform.Find("Worm.SpawnPoint").transform.position, Quaternion.identity);
                break;

            case 3 -1:
                    var lose1live4 = Instantiate(lose1livePrefab, transform.Find("Worm.SpawnPoint").transform.position, Quaternion.identity);
                break;

            case 3 -2:
                    var lose2lives4 = Instantiate(lose2livesPrefab, transform.Find("Worm.SpawnPoint").transform.position, Quaternion.identity);
                break;

            case 2 -1:
                    var lose1live5 = Instantiate(lose1livePrefab, transform.Find("Worm.SpawnPoint").transform.position, Quaternion.identity);
                break;

            case 2 -2:
                    var lose2lives5 = Instantiate(lose2livesPrefab, transform.Find("Worm.SpawnPoint").transform.position, Quaternion.identity);
                break;

            case 1 -1:
                    var lose1live6 = Instantiate(lose1livePrefab, transform.Find("Worm.SpawnPoint").transform.position, Quaternion.identity);
                    LOSELIVES = 6;
                break;
        }   
}

Thanks XD

Hey there, a few things:

That warning is almost certainly caused by: transform.Find("Worm.SpawnPoint").transform.position

For one, transform.Find returns a transform, so you're looking for transform.transform.position. But the warning is saying that it can't find "Worm.SpawnPoint".

You should declare the Worm Spawnpoint earlier. If it exists before the scene is loaded, you could link to it in the same way as you've done with the lose life prefabs. Otherwise, you should look it up once, rather than every time you're hit.

i.e.

function Start()
{
   GameObject.Find("Worm.SpawnPoint").transform.position
}

You'll also need to ensure that "Worm.SpawnPoint" is the full, complete, unique name of the GameObject you wish to find.


Aside from the error you're getting... your switch statement seems strange. You're passing in 'LOSELIVES' and checking if it is "6 -1" or "6 -2" or "5 -1", etc. 6-2 evaluates to '4', so it will never get to 5-1 (also 4). I assume you actually want to be checking how much damage you took - you'll need to pass that value through instead. Looking at that excerpt, it doesn't matter how many lives you currently have as to which prefab will appear.

You might also want to consider using GUIText to display the hit damage, rather than 2 prefabs. There have been a few questions already asked about how to do this; here's one example.

Hope this helps!