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;
}
}
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.
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.
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. >.>
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.