Still getting nowhere with this.
Although I did have a ‘doh’ moment.
I was loading a mainmenu scene. Then opening my scene2.
In scene2 I was running the levelgenerator to create a list of levelPieces.
That was all working, my issues was when I came back to the mainmenu and I got the object destroyed message.
Then it dawned on me that in my mainmenu I hadn’t created my levelpiece or levelGenerator object, beacuse I dont need them there, its not part of the game. But I guess if they aren’t there, it cant keep them alive.
So I tried creating the LevelPiece object and the LevelGenerator object in my main menu also.
Now this means when I run the main menu, I get all my levelpieces created (on my menucanvas by default, I’ll need to move them onto my other canvas for display later)
However, when clicking the button to go through to scene2 it dies with my favourite error message.
MissingReferenceException: The object of type ‘LevelPiece’ has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
It does this as soon as any object in scene2 tries to access the List that was created in the menu scene, eg
void MoveTowards(){
float step = speed * Time.deltaTime;
// Check to make sure we should be moving towards
if (LevelGenerator.instance.pieces[currentTarget].displayOnly == true){
// Move on to next piece
IncrementCurrentPiece ();
}
target = LevelGenerator.instance.pieces[currentTarget].transform.position;
So where previously I was creating the List of objects in scene2 and they were vanishing when going back to mainmenu, I’ve now got the opposte issue in that they create in mainmenu scene and die when going to scene2.
I’m sure there is something really simple stopping this from working, but I have no idea.
Can someone please look at this code and let me know what I’m doing wrong. I know its a silly newbie mistake, but I cant work it out. I tried splitting it as below so the declaration only is done in mainmenu, and in scene2 it does all the work on it, but it still seems to fail.
I just dont know how to keep this list of object persistant through the 2 scenes.
Please help me. This is driving me mad now.
Thanks
LevelGenerator Class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LevelGenerator : MonoBehaviour {
public static LevelGenerator instance;
public Transform levelStartPoint;
public List<LevelPiece> pieces = new List<LevelPiece> ();
public List<TextPiece> textPieces = new List<TextPiece> ();
public LevelPiece LevelPiece;
public TextPiece TextPiece;
private StandardVars standardVars = new StandardVars();
private int lastPiece;
void Awake() {
if (instance == null) {
DontDestroyOnLoad (gameObject);
instance = this;
} else {
if (instance != this) {
Destroy (gameObject);
}
}
}
void Start () {
if (Application.loadedLevel ==0) {
//lastPiece = 0;
GenerateInitialPieces ();
GenerateTextPieces ();
PrintValues ();
}
if (Application.loadedLevel >=1) {
//Debug.Log ("Calling Sprite Update");
UpdateSprites ();
}
}
LevelPiece CLass
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LevelPiece : MonoBehaviour {
void Start () {
// Just variable definitions
etc
etc
}
// Update is called once per frame
void Update () {
}
void Create () {
}
}