- Home /
 
Don't Destroy on Load Function is not working for me, and...
First off, I have only been using Unity for a couple of days... and I am completely new to scripting (not saying Im not willing to try to write something myself, only saying If you go all "yoda on me I may not have a clue what you're saying cause I'm still a fish")
// I have this .js
function Awake(){
DontDestroyOnLoad (transform.gameobject);
}
attached to my Player my player has tag : Player
//I have this .js
function OnLevelWasLoaded(level : int) { if (level == 1){ GameObject.FindGameObjectsWithTag("Player"); transform.postiton = Vector3(-30,1.5,0); transform.rotation = Quaternion(0,90,0,0); }
}
//My huge issue....
When the new scene is loaded.. My Player from the previous scene doesn't spawn or whatever u want to call it.. basically leaving me with just a camera that I had already positioned in the new scene, with a smooth follow script targeting my Player who somehow got lost in the time-space continuum/blackhole of unity engine.. and of corse my beautiful un-explorable terrain.. I'm so very confused and tired. The new scene is (1) in the Build settings..
Also, I do not wish to have a carbon copy of player in my starting scene.. as I am making an mmorpg style game at the moment and dont want thousands of players (assuming my game is epic when finished) standing at the trigger... So if my methodology here will do that can you please steer me in a good direction..
Also*2 Is there a way to look up last level loaded in memory like example : lastloadedlevel == 0?
Oh, and at what point am I trusted enough to not have all my posts reviewed by an Admin if ever.. I'm an Impatient Person? Thanks so much peoples hopefully Yall can help me.
Answer by Panik.Studios · Aug 21, 2012 at 08:18 AM
Answer
This is to Be Placed on the "Player"
 function Awake(){
 
 DontDestroyOnLoad (this);
 
 }
 
               And this Code ...
 function OnLevelWasLoaded(level : int)
 {
     if (level == 1){
         transform.position = Vector3(-30,1.5,0);
         transform.rotation = Quaternion (0,90,0,0);
     }
 }
 
               This Script is to be placed on the event trigger (box collider anything ect)
 function OnTriggerEnter (other:Collider)
 {
     if(other.CompareTag("Player"))
     {
         Application.LoadLevel("Cave of Discovery");
     }
 }
 
              Answer by Panik.Studios · Aug 19, 2012 at 10:32 AM
Ok I added debug.log("script works") to the code and it didnt log it so something is going foofy in the LoadLevelscript for sure?
So this line:
"DontDestroyOnLoad (transform.gameobject);"
isn't being called?
No I don't believe so. Otherwise it would say "Script Works" in the editor right?
I don't know, I can't see your code. What matters it that it's not being called, thus it can't take effect. You need to debug your code, and see why it's not reaching that line etc.
I got everything working now.. do you know how I can close this question?
You don't need to close the question, just mark the answer that you accept. Here is my generic post for new users =]
I wish this was made clearer for new users, so just some tips on using this 'site (for ALL new users) :
How to accept an answer :
On the left-hand-side of the Answer box , there are the following icons :
: Thumb Up : Number (of votes) : Thumb Down : A Tick :
If an answer worked for you , click on the 'Tick' , the answer should now be highlighted in green. If you like an answer on Any question , you can click on the Thumb UP , the thumb should now be highlighted in green , and the number of votes should rise by 1.
How to reply to an answer / post a comment :
To make a comment , USE the [add new comment] button, a window then opens to type in. The answer fields are for ANSWERS, so unless you are answering your own question , DON'T write in an answer box. This helps the 'site work properly, especially when other people are searching for answers, and want to read answers , not comments.
IF your question changes slightly while commenting and reading comments , EDIT the original Question, so anyone reading from the beginning knows what you are asking.
This will make for a happy experience for everyone. I made mistakes starting on this 'site too, but everyone is helpful if you learn and change these habits.
Following these simple steps helps the website work , and other readers to find answers also.
Happy Coding =]
the FAQ appears at the top of the page : http://answers.unity3d.com/page/faq.html
also : http://answers.unity3d.com/questions/133869/how-to-ask-a-good-question.html
Your answer
 
             Follow this Question
Related Questions
DontDestroyOnLoad() does not seem to be working. 3 Answers
random respawn and respawn delay 1 Answer
How to make a spawner? 1 Answer
How to respawn items along with player 1 Answer
Spawn and Destroy problem snake like 2 Answers