multiple spawn locations problem

i am currently making a game which has several levels that the player can go to by entering a trigger. However the problem is that when returning to the first level the player is taken back to the start location and i would like it to spawn near the door its just come from. This is the code i currently have and Spawn 1 is the place which i would like the player to spawn at first when coming from the menu scene. The error that unity is giving me at the moment is BCE0044: expecting :, found ‘=’.

var previousLevel: int;
var Spawn1: Transform;
var Player: GameObject;

function Start () {
player = GameObject.FindWithTag("Player").transform;
}

function OnLevelWasLoaded (level : int) {
Debug.Log(level);
	if (previousLevel == 1);
	Debug.Log("level1 loaded");
	{
		Transform.position = Spawn1.position;
	}
}

Thanks for any help.

Not sure if it specifically relates to your spawn locations issue, but you have a malformed IF statement…

var previousLevel : int;
var Spawn1 : Transform;
var Player : GameObject;

function Start () {
 player = GameObject.FindWithTag("Player").transform;
}

function OnLevelWasLoaded(level : int) {
	Debug.Log(level);
	if (previousLevel == 1) {
		Debug.Log("level1 loaded");
		Transform.position = Spawn1.position;
	}
}

thanks for that. it got rid of the error i was having. How would i be able to change the script so that instead of looking for a specific gameobject, it would go to a predefined location?

To achieve this: “i would like it to spawn near the door its just come from”

You could use a “storage” script that is never destroyed (don’t destroy on load) - and in this, store the transform you wish the player to move to when entering the next level.

You could set this as a … Vector3 or a string with a tag or object name or… anything really…

You would set this in the trigger of your teleporter or such before loading the next level.

Then, upon level load, your transform.position will be set to the position saved in your undestroyable storage script.

That’ll be the ticket.

-JFFM

Is there any chance you can help me with the script? im struggling to get my head around the scripts. Thanks

You can set a point directly in a Vector3 variable (easiest to do this from the inspector) and then just assign that to transform.position:-

var spawnPos: Vector3;
  ...

transform.position = spawnPos;

thanks for the reply. i got the position working now which is a step in the right direction. however im still not sure how to get the previous level int working properly. iv followed the last solution in this thread but can’t place the second piece in without it coming up with a “insert a semicolon at the end” error.

Code used in the menu

if(GUI.Button(Rect(512, 384,128, 32), "Play")) {
		PlayerPrefs.SetInt( "previousLevel", Application.loadedLevel );
		Application.LoadLevel("Outside");
	}

Spawn script code

private var previousLevel : int;
var Spawn1 : Vector3;
var SpawnMain : Vector3;
var SpawnSmall : Vector3;
var SpawnSport : Vector3;
var SpawnSport2 : Vector3;
var Player : GameObject;

function OnLevelWasLoaded(level : int) {
	if (previousLevel == 0) {
		transform.position = Spawn1;
		}
	if (level == 5) {
		transform.position = SpawnSport2;
		}
	if (previousLevel == 5) {
		transform.position = SpawnSport;
	}
 }

I’m guessing you have bad syntax. Can you post the full code of the file throwing the error?

private var previousLevel : int;
var Spawn1 : Vector3;
var SpawnMain : Vector3;
var SpawnSmall : Vector3;
var SpawnSport : Vector3;
var SpawnSport2 : Vector3;
var Player : GameObject;

function OnLevelWasLoaded(level : int) {
int previousLevel = PlayerPrefs.GetInt( "previousLevel" );
	if (previousLevel == 0) {
		transform.position = Spawn1;
		}
	if (level == 5) {
		transform.position = SpawnSport2;
		}
	if (previousLevel == 5) {
		transform.position = SpawnSport;
	}
 }

its giving the UCE0001 error on the “int previousLevel = PlayerPrefs.GetInt( “previousLevel” );” line

No need for the int to be there in JavaScript. You’ve already cast the variable type at the top.

previousLevel = PlayerPrefs.GetInt(“previousLevel”); will suffice.

thanks for the replys, but tis still not getting the level index of the last level for some reason. the error message has no gone but the var is not updating. Sorry for being a pain about this.

Can you make a Debug.Log call whenever you save the level number or read it back? It’s possible you have a minor oversight/bug that’s preventing these methods from being even being called. I’ve been there before. >.> :stuck_out_tongue:

i’ve added a debug.log on the script which loads a new level and each time i load a level its still returning 0, which is the menu index instead of 2 which is the level used for outside.

Edit: thanks for all your help guys. figured out what was wrong and can’t believe it was so simple. i had forgot to add a setint when loading a level so thats why it was staying at 0. sorry about being so silly.