Im made my first succsessful script!!! + Im stuck on the next bit...

I made my first successful script without baddgering anyone about the answer!!! I was incredably rewarded!! I made a teleport script where i can say which map to teleport to!!! :smile::smile::smile::smile::smile::smile:

All in Unityscript

var map : int;
function OnTriggerEnter (other: Collider){
Application.LoadLevel(map);
}

I am very proud of myself!!!

UPDATE

Got it!
Ok what i have found out is that the script is certainly (had a theory from past “not real scripting” engine 001 scripting) that the script is taking the player to 0,0,0 on the map the script is on but not on the new map!

So the last (i hope) questions is, is it possable to enforce it onto the new map, (have it work on the new map or set it so it works globe-aly) if so how?

Oh and incase anyone was hinting that putting a script onto the new map placeing the player at a certain point, problem is the player will enter the map at different locations and needs to be placed at the correct location. Lets say you come out of your bedroom in the morning and you somehow end up outside your house (arkward huh?) yeah i dont want that. :slight_smile:

var map : int;
var gridPosition = Vector3(1, 1, 1);
var player : GameObject;

function OnTriggerEnter (other: Collider){
Application.LoadLevel(map);
player.transform.position =(gridPosition);
Debug.Log ("Success!");
}

function Start () {
player =GameObject.Find("3rd Person Controller");
}

OLD
Anyway my next plan was to add another bit onto it describing where exactly along the axis the player will end up…
This is what i got.

var gridPosition = Vector3(1, 1, 1);
var player : GameObject;

function OnTriggerEnter (other: Collider){
Application.LoadLevel(map);
player.transform.position = Vector3(gridPosition);

}
//player.transform.position = Vector3(25, 25, 25);
//Debug.Log ("Success!");
//gridPosition
//Transform.position(gridPosition)
//function Update ()   if (Input.GetKeyDown("e")
//var player =GameObject.Find("3rd Person Controller");

I bet the answer is in one of the notes.
And i bet this is it…but when the script is done the gameplay goes abit funny. I’ll explain later.

var player =GameObject.Find("3rd Person Controller");
//For some reason i have to shorten the var to...
var player =GameObject;

…because the console says something about “Find can only be called from the main thread.” I didnt understand what the console was saying, something to do with contructor and arguments, looked it up in script ref, nothing that suits it. So in variables i have to drag the 3rd person controller manualy.

player.transform.position = Vector3(gridPosition);
//and thats the rest of the script...

When the scripy finishes the gameplay goes funny.Like the cammra becomes detached and other stuff i dont understnad. I’ll explain futher later on.
Tackle one problem at a time eh?[/PHP]

Is player destroyed when the new level is loaded? This happens by default so you’d have to call DontDestroyOnLoad() on player in Start(). Also - you probably only want to move the player once the level has loaded so it would be better to create an empty GameObject in the ‘map’ scene and attach a script to it that moves the player in Start().

No it dosnt get destroyed. There was no camera nor player, i suspected they would both get loaded into the level the script is taking them. So
guess i was wrong. Anyway i inserted both a camera and player and also edited the script.

var map : int;
var gridPosition = Vector3(1, 1, 1);
var player : GameObject;

function OnTriggerEnter (other: Collider){
Application.LoadLevel(map);
player.transform.position =(gridPosition);
}

Unfortunatly the grid position script didnt take the player to the desired cords…

I shall give it a try…

Well I’m guessing that since you’re setting the player outside of a method the code ends up in the constructor. The constructor is called as soon as Unity (the game or the editor) loads the scene, since there is no guarantee that the player will be loaded before or after this object there would be a chance it worked a chance it wouldn’t work. So in order to prevent your code from looking right but not working; Unity intercepts this and marks it as an error.

This way you know that something is wrong and can start looking for the answer.

Your code should look something like this:

var player : GameObject;

function Start()
{
  player =GameObject.Find("3rd Person Controller");
}

Start is called by Unity when the GameObject is put into play, either when its created at run time or when the game has loaded all objects and starts “play” mode.

Cheers,
UnLogick

From your post, it would seem that your’e just copying and pasting lines all over the place without rhyme or reason and betrays a lack of basic programming knowledge. I think you’re attempting something way beyond your capabilities and suggest you take a step back and learn how to program first. There’s a lot of free tutorials online and “learn to program in 21 days” type of books out there, and it will help you immensely in trying to sort out the very basic problems you’re encountering. Mashing code together from several sources and posting it so that others can do the work for you isn’t going to help you. good luck!

Nope not mashed togeather, i acculy took the time to sit down and look into it. All the above code i understand and im quite confiddent to find the problem but thats the thing i cant find it…lol so at the mo im clueless…all im trying to do is to get the vector var working after the map has been loaded…anyway, try me…

Ok got that, but no difference for the vector. I’ll keep it just incase…

Ok i just made a new script with the vector teleport in it. It works in the same map its attached to but it dosnt want to work with the teleport script…

Got it!
Ok what i have found out is that the script is certainly (had a theory from past “not real scripting” engine 001 scripting) that the script is taking the player to 0,0,0 on the map the script is on but not on the new map!

So the last (i hope) questions is, is it possable to enforce it onto the new map, if so how?

Oh and incase anyone was hinting that putting a script onto the new map placeing the player at a certain point, problem is the player will enter the map at different locations and needs to be placed at the correct location. Lets say you come out of your bedroom in the morning and you somehow end up outside your house (arkward huh?) yeah i dont want that. :slight_smile:

Got it!

var map : int; //Which numbered map you want to load/teleport to
var gridPosition = Vector3(1, 1, 1); //Where on the new map you want to go to
var player : GameObject; //What object you want teleported (player)

function OnTriggerEnter (other: Collider){
Application.LoadLevel(map);
DontDestroyOnLoad(player);
player.transform.position =(gridPosition);
Debug.Log ("Success!");

}

function OnLevelLoaded (map){

player.transform.position =(gridPosition);