x


Loading next level, but next level never gets incremented.

Ok so now my problem is about loading levels. if you scroll down to the die function it is supposed to load the nextLevel and then increase nextlevel bu one so that next time it loads the level after this one. But it keeps loading the same level over and over. I have added all levels to build settings in order.

var target;
//target = transform.Find("Character");
var damp : int = 3; 
var attackRange = 3; // safe distance from the player
var followRange = 50; //distance required for enemy to start following
var bulletPrefab : Transform; 
var savedTime = 0;
var explosionPrefab : Transform;
var killsNeeded = 10;
var nextLevel = 2;
var shootSound : AudioClip;
var dieSound : AudioClip;

var hitPoints = 3;

function Update () 
{   
target = GameObject.FindWithTag("Player");

if((followRange > Vector3.Distance(target.transform.position, transform.position )) &&     (attackRange != Vector3.Distance(target.transform.position, transform.position )))     // if the enemy is closer than the follow range
{
    follow(target);
}

var seconds : int = Time.time;
var oddeven = (seconds % 2);

if ((attackRange >= Vector3.Distance(target.transform.position, transform.position )) &&(oddeven))
    Shoot(seconds);
}

function follow(target)
{
    if( attackRange < Vector3.Distance(target.transform.position, transform.position))   //if enemy is farther than attack range
{
    transform.LookAt(target.transform); //look at target
    transform.Translate (0,0,6*Time.deltaTime, Space.Self ); //move towards target
}
}


function Shoot(seconds)
{
    if(seconds != savedTime)
{
    audio.PlayOneShot(shootSound);
    var shoot = Instantiate(bulletPrefab, transform.Find("BulletSpawn").transform.position, Quaternion.identity);
    shoot.gameObject.tag = "enemyProjectile";
    shoot.rigidbody.AddForce(transform.forward * 1000);

    savedTime = seconds;
}
}

function OnTriggerEnter(hit : Collider)
{
if(hit.gameObject.tag == "fallout")
{
     Die();
}

if(hit.gameObject.tag == "playerProjectile")
{
    hitPoints -= 1;
    //Destroy(hit.gameObject);

    if(hitPoints < 1)
    {
        Die();
    }
}
}

function Die()
{
    audio.PlayOneShot(dieSound);
    MoveAround.kills++;
    botMaker.AlienCount--;
    //MoveAround.scored = true;

    if (MoveAround.kills >= killsNeeded)
    {
        Application.LoadLevel(nextLevel);
        healthControl = 6;
        MoveAround.kills = 0;
        botMaker.AlienCount = 0;
        nextLevel++;
}

Destroy(gameObject);
var explode = Instantiate(explosionPrefab, gameObject.transform.position, Quaternion.identity);
}
more ▼

asked Mar 29 '11 at 08:54 PM

networkZombie gravatar image

networkZombie
244 9 11 20

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

I don't see a DontDestroyOnLoad (gameObject) in there. If this object (I'm assuming it's some enemy behavior) gets destroyed when a new level is loaded, none of the code beyond Application.LoadLevel(nextLevel); will count. You either need to separate the functionality for loading new levels into a GameObject that lives between scenes or make nextLevel static and make sure it's executed before the object is destroyed (perhaps using Application.LoadLevel (nextLevel++);.

more ▼

answered Mar 29 '11 at 09:15 PM

burnumd gravatar image

burnumd
3.4k 22 35 71

(comments are locked)
10|3000 characters needed characters left

Both the above answers work properly. what I did was I declared the nextlevel variable in another script which is attached to a game object that never gets destroyed.

more ▼

answered Mar 29 '11 at 11:12 PM

networkZombie gravatar image

networkZombie
244 9 11 20

(comments are locked)
10|3000 characters needed characters left

The variable 'nextLevel' only lives as long as the object you attach this script to. When you destroy it your changes are gone. You need to store that value somewhere global or static.

http://unity3d.com/support/documentation/ScriptReference/index.Member_Variables_26_Global_Variables.html

more ▼

answered Mar 29 '11 at 09:14 PM

flaviusxvii gravatar image

flaviusxvii
3k 13 19 43

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5272
x3570
x3418
x2030
x329

asked: Mar 29 '11 at 08:54 PM

Seen: 1276 times

Last Updated: Mar 29 '11 at 09:11 PM