Hey,i have made 10 levels and i put a Dont destroy on load script for some objects like the player and the life and etc…
and i have attached another script says, that if i reach level 9, disable “Dont destroy on load” script for every object so that in the next level the objects will be destroyed when loading.
when i reach level 9, it does disable every dontDestroyOnLoad script but when i load level 10, the object stay…
here some scripts if it helps…
DontDestroyOnLoad: (Attached to every object i wanted)
function Awake()
{
DontDestroyOnLoad (this);
}
and the level change script: and inside it there is the script that disable the DontDestroyOnLoad script when getting to level 9
var Level:int = 1;
var LevelUP = false;
var Player:Transform;
var Timer:Transform;
var GUILIFE:Transform;
var GUIHELPER:Transform;
var GUISPIN:Transform;
var GUIFPS:Transform;
var OKEOKE:Transform;
var MainCamera:Transform;
var CheckPoint:Transform;
function OnControllerColliderHit(hit:ControllerColliderHit)
{
if(hit.gameObject.tag == "NextLevel")
{
Destroy(hit.gameObject);
Level = Level + 1;
LevelUP = true;
}
}
function Update()
{
if(Level == 2)
{
if(LevelUP)
{
LevelLoadFade.FadeAndLoadLevel("World1 Level 2",Color.white, 0.5);
LevelUP = false;
}
}
if(Level == 3)
{
if(LevelUP)
{
LevelLoadFade.FadeAndLoadLevel("World1 Level 3",Color.white, 0.5);
LevelUP = false;
}
}
if(Level == 4)
{
if(LevelUP)
{
LevelLoadFade.FadeAndLoadLevel("World1 Level 4",Color.white, 0.5);
LevelUP = false;
}
}
if(Level == 5)
{
if(LevelUP)
{
LevelUP = false;
LevelLoadFade.FadeAndLoadLevel("World1 Level 5",Color.white, 0.5);
}
}
if(Level == 6)
{
if(LevelUP)
{
LevelLoadFade.FadeAndLoadLevel("World1 Level 6",Color.white, 0.5);
LevelUP = false;
}
}
if(Level == 7)
{
if(LevelUP)
{
LevelLoadFade.FadeAndLoadLevel("World1 Level 7",Color.white, 0.5);
LevelUP = false;
}
}
if(Level == 8)
{
if(LevelUP)
{
LevelLoadFade.FadeAndLoadLevel("World1 Level 8",Color.white, 0.5);
LevelUP = false;
}
}
if(Level == 9)
{
if(LevelUP)
{
Player.GetComponent("DontDestroyOnLoad").enabled = false;
Timer.GetComponent("DontDestroyOnLoad").enabled = false;
GUILIFE.GetComponent("DontDestroyOnLoad").enabled = false;
GUIHELPER.GetComponent("DontDestroyOnLoad").enabled = false;
GUISPIN.GetComponent("DontDestroyOnLoad").enabled = false;
GUIFPS.GetComponent("DontDestroyOnLoad").enabled = false;
OKEOKE.GetComponent("DontDestroyOnLoad").enabled = false;
MainCamera.GetComponent("DontDestroyOnLoad").enabled = false;
CheckPoint.GetComponent("DontDestroyOnLoad").enabled = false;
LevelLoadFade.FadeAndLoadLevel("World1 Level 9",Color.white, 0.5);
LevelUP = false;
}
}
if(Level == 10)
{
if(LevelUP)
{
Application.LoadLevel("WorldSelection(1-2)");
LevelUP = false;
}
}
}
I think the problem is, you call “DontDestroy” in the Awake function. This is called before the Update function that will disable your DontDestroy script.
So before you disabled them, they already registered theirself as “dont destroy me at level change”.
You could do something like this:
function Awake()
{
if(Application.loadedlevel != 9) // Or whatever level this is
DontDestroyOnLoad (this);
}
So it will only call this line when the current level is not level 9.
I think the problem is, you call “DontDestroy” in the Awake function. This is called before the Update function that will disable your DontDestroy script.
So before you disabled them, they already registered theirself as “dont destroy me at level change”.
You could do something like this:
function Awake()
{
if(Application.loadedlevel != 9) // Or whatever level this is
DontDestroyOnLoad (this);
}
So it will only call this line when the current level is not level 9.
thanks for replaying, i will try this
I think the problem is, you call “DontDestroy” in the Awake function. This is called before the Update function that will disable your DontDestroy script.
So before you disabled them, they already registered theirself as “dont destroy me at level change”.
You could do something like this:
function Awake()
{
if(Application.loadedlevel != 9) // Or whatever level this is
DontDestroyOnLoad (this);
}
So it will only call this line when the current level is not level 9.
Well i tried this now, but when im in level 9 (20 in index) the script doesnt even get disabled…
so its the awakes problem?
No this won’t disable the script but it should prevent the object to reappear in level 10, did you try it?
If you want to disable it, just use an
function Awake()
{
if(Application.loadedlevel != 9) // Or whatever level this is
DontDestroyOnLoad (this);
else
this.enabled = false;
}
The awake function will only get called the first time it is loaded. So disabling the component afterwards doesn’t do much.
But you can just destroy the objects manually whenever you want.
something like this:
var objects : DontDestroyOnLoad[] = FindObjectsOfType(DontDestroyOnLoad);
for (var obj : DontDestroyOnLoad in objects)
{
Destroy(obj.gameObject);
}
The awake function will only get called the first time it is loaded. So disabling the component afterwards doesn’t do much.
But you can just destroy the objects manually whenever you want.
something like this:
var objects : DontDestroyOnLoad[] = FindObjectsOfType(DontDestroyOnLoad);
for (var obj : DontDestroyOnLoad in objects)
{
Destroy(obj.gameObject);
}
Thanks but thats kinda complecated for me…
i have only started learning like 3 months ago and by myself so can you explain me abit of that code?
but ill try it now tho.
The awake function will only get called the first time it is loaded. So disabling the component afterwards doesn’t do much.
But you can just destroy the objects manually whenever you want.
something like this:
var objects : DontDestroyOnLoad[] = FindObjectsOfType(DontDestroyOnLoad);
for (var obj : DontDestroyOnLoad in objects)
{
Destroy(obj.gameObject);
}
I get a massage saying:
“The best overload for the method ‘UnityEngine.Object.FindObjectsOfType(System.Type)’ is not compatible with the argument list ‘(callable(UnityEngine.Object) as void)’.”
and its on the line of
var objects : DontDestroyOnLoad[] = FindObjectsOfType(DontDestroyOnLoad);
oops, sorry I never use Javascript and it’s late
What that code is supposed to do is find all objects that have this component on it.
But that isn’t the important part. I mean you can just Destroy the gameObjects after you reach level 10.
Destroy(Player.gameObject);
Destroy(Timer.gameObject);
etc.
oops, sorry I never use Javascript and it’s late
What that code is supposed to do is find all objects that have this component on it.
But that isn’t the important part. I mean you can just Destroy the gameObjects after you reach level 10.
Destroy(Player.gameObject);
Destroy(Timer.gameObject);
etc.
Yes i have done it like half hour ago, forgot to say, thanks for leading me to the answer i just needed to hear the destroy part