Search Unity

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

Discussion in 'Scripting' started by Ethanbf3, Apr 11, 2012.

  1. Ethanbf3

    Ethanbf3

    Joined:
    Feb 15, 2012
    Posts:
    153
    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!!! :D:D:D:D:D:D

    All in Unityscript

    Code (csharp):
    1. var map : int;
    2. function OnTriggerEnter (other: Collider){
    3. Application.LoadLevel(map);
    4. }
    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. :)

    Code (csharp):
    1. var map : int;
    2. var gridPosition = Vector3(1, 1, 1);
    3. var player : GameObject;
    4.  
    5. function OnTriggerEnter (other: Collider){
    6. Application.LoadLevel(map);
    7. player.transform.position =(gridPosition);
    8. Debug.Log ("Success!");
    9. }
    10.  
    11. function Start () {
    12. player =GameObject.Find("3rd Person Controller");
    13. }
    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.
    Code (csharp):
    1.  
    2. var gridPosition = Vector3(1, 1, 1);
    3. var player : GameObject;
    4.  
    5. function OnTriggerEnter (other: Collider){
    6. Application.LoadLevel(map);
    7. player.transform.position = Vector3(gridPosition);
    8.  
    9. }
    10. //player.transform.position = Vector3(25, 25, 25);
    11. //Debug.Log ("Success!");
    12. //gridPosition
    13. //Transform.position(gridPosition)
    14. //function Update ()   if (Input.GetKeyDown("e")
    15. //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.
    Code (csharp):
    1.  
    2. var player =GameObject.Find("3rd Person Controller");
    3. //For some reason i have to shorten the var to...
    4. var player =GameObject;
    5.  
    ...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.

    Code (csharp):
    1. player.transform.position = Vector3(gridPosition);
    2. //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]
     
    Last edited: Apr 12, 2012
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    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().
     
  3. Ethanbf3

    Ethanbf3

    Joined:
    Feb 15, 2012
    Posts:
    153
    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.

    Code (csharp):
    1. var map : int;
    2. var gridPosition = Vector3(1, 1, 1);
    3. var player : GameObject;
    4.  
    5. function OnTriggerEnter (other: Collider){
    6. Application.LoadLevel(map);
    7. player.transform.position =(gridPosition);
    8. }
    Unfortunatly the grid position script didnt take the player to the desired cords...

    I shall give it a try...
     
  4. UnLogick

    UnLogick

    Joined:
    Jun 11, 2011
    Posts:
    1,745
    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:

    Code (csharp):
    1. var player : GameObject;
    2.  
    3. function Start()
    4. {
    5.   player =GameObject.Find("3rd Person Controller");
    6. }
    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
     
  5. diablo

    diablo

    Joined:
    Jan 3, 2011
    Posts:
    736
    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!
     
  6. Ethanbf3

    Ethanbf3

    Joined:
    Feb 15, 2012
    Posts:
    153
    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...
     
  7. Ethanbf3

    Ethanbf3

    Joined:
    Feb 15, 2012
    Posts:
    153
    Ok got that, but no difference for the vector. I'll keep it just incase...
     
  8. Ethanbf3

    Ethanbf3

    Joined:
    Feb 15, 2012
    Posts:
    153
    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...
     
  9. Ethanbf3

    Ethanbf3

    Joined:
    Feb 15, 2012
    Posts:
    153
    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. :)
     
  10. Ethanbf3

    Ethanbf3

    Joined:
    Feb 15, 2012
    Posts:
    153
    Got it!
    Code (csharp):
    1. var map : int; //Which numbered map you want to load/teleport to
    2. var gridPosition = Vector3(1, 1, 1); //Where on the new map you want to go to
    3. var player : GameObject; //What object you want teleported (player)
    4.  
    5. function OnTriggerEnter (other: Collider){
    6. Application.LoadLevel(map);
    7. DontDestroyOnLoad(player);
    8. player.transform.position =(gridPosition);
    9. Debug.Log ("Success!");
    10.  
    11. }
    12.  
    13. function OnLevelLoaded (map){
    14.  
    15. player.transform.position =(gridPosition);