- Home /
Question by
BDX777 · Mar 17, 2015 at 08:08 PM ·
bugclonedontdestroyonloadapplication.loadlevel
Application.LoadLevel object clone spam
Hi, I tried DontDestroyOnLoad and Application.LoadLevel and the object is constantly created in the second level. Why is this?
using UnityEngine;
using System.Collections;
public class BacktrackingTest : MonoBehaviour
{
public float float1 = 420;
public float float2 = 9000;
private bool LevelLoaded = false;
void Start ()
{
DontDestroyOnLoad(gameObject);
//Data transfer between unity scenes test, does it subtract twice?
float2 = float2 - float1;
}
void FixedUpdate ()
{
if(Time.time>5 && LevelLoaded==false)
{
Application.LoadLevel(1);
LevelLoaded=true;
}
}
}
Comment
maybe call DontDestroyOnLoad(gameObject) from the Awake() function?
Nah it still creates itself constantly in the second level.
I think I know what's causing it. The script would keep loading the same level, and since it's an object that won't destroy itself, it would keep loading the same level, and thus duplicating itself with the same LevelLoaded check, creating more and more objects with the same script. Have a level load button ins$$anonymous$$d. :D
Yeah I tested it like this:
if(Input.GetButtonDown("Jump"))
{
Application.LoadLevel(Application.loadedLevel+1);
}
Now I understand why this was happening, and why without DontDestroyOnLoad() it would flicker.