- Home /
Help with loading new levels - restarting scripts
I have created the start and first level of a game. I would like to add several more levels to the game but need some help. The game is a simple shooter game that I should be able to reuse almost all the scripts in the subsequent levels but I need then to reset. Is this possible and if so how do I do such a thing. Also is it possible to set the var in the scripts different for each level without using a copy of the script. Please let me know what I need to post if you do not understand the question.
Thanks
Monte
Thanks for the help guys. I think I understand. I made an exact copy of level one by duplicating it and when I loaded level two all it loaded as if all ready played. In other words exactly like level one finished. I hope that makes sense. So if the scripts reset why would that happen?
Ok guys I think I have tried everything I know how to do at my early stage of learning. The scripts are not resetting. If I understand it right I should be able to make an exact duplicate of level one as level two and it should work. but all my UI is staying exactly as it ends in level one. The new scene is loading with all the old information in the UI. Again I can post what ever would help or the whole game as a zip if that is what you need.
Any help would be greatly Appreciated.
$$anonymous$$onte
Answer by Kirbyrawr · Sep 14, 2012 at 02:27 PM
The scripts resets when you change to another scene , what's your problem?
Just to make it clear: the scripts themselves do not reset. But the GameObject on a scene are reloaded setting their components back to the original state stored in the scene file.
Answer by ChrisAllen · Sep 14, 2012 at 02:33 PM
the script will reset if you're loading them into another scene, unless you are using doNotDestroyOnLoad().
On your second point. If you declare the variables you want to change outside of your Update() function (usually at the very top of the script),they will appear in the inspector for the object they are attached to. Then you can change the variable values in the inspector and it will only affect that particular game object.
so your script would look like this
var levelNumber : int;
var health : int;
var numberEnemies : int;
function Update()
{
//your update scripts here
}
Then in the inspector you will have some options in the script component called
Level Number
Health
Number Of Enemies
you can reuse the same script in each level setting these variables throught the inspector (you can set the values of the variables in the script but the inspector version overwrites whatever is in the actual script when you run the game)
I hope this makes some sort of sense, and helps you out
Answer by Kryptos · Sep 14, 2012 at 03:01 PM
Put the script on different prefab and use one prefab per scene.
Or create a unique prefab, add an instance of this prefab in each scene and override their value in the Inspector like @chrisallen suggested.
One way or the other, you don't need to copy/paste your script.
Just to be clear you are saying use the same script and a different prefab. so Copy the prefab then but the same script on it and I can change the values in the inspector. so the script becomes an instance of the prefab and therefore does not effect the uses of the script.
I assume then the prefab should not be created with the script already attached?
thanks
$$anonymous$$onte
you can attach the script to the prefab, but you will need to change the inspector variables for each instance of that object (when you drag the prefab into your scene)
Answer by montedre · Sep 14, 2012 at 04:25 PM
Thanks for the help guys. I think I understand. I made an exact copy of level one by duplicating it and when I loaded level two all it loaded as if all ready played. In other words exactly like level one finished. I hope that makes sense. So if the scripts reset why would that happen?
Your answer
![](https://koobas.hobune.stream/wayback/20220613081021im_/https://answers.unity.com/themes/thub/images/avi.jpg)