公共静态字典中的游戏对象<字符串,游戏对象>在 Unity 中的场景更改时销毁



第一个形式声明:程序设计语言:统一中的C#(MonoBehavior)我的技能水平:有点傻(不到半年的c#经验)。

我正在制作一个破方块游戏(Arkanoid),并正在制作一种成就系统。游戏是一次性的,当你关闭网络构建时,所有内容都会被删除(没有缓存,没有文件保存,没有可序列化的类)。

成就体系包括:"Acheivement"类:许多变量和一些跟踪进度的方法。它附加到一个同名的空游戏对象上的脚本。普通类。

"AManager"类。初始化"Acheivement"类的一组副本的类,给它们单独的值,并将它们添加到下面提到的字典中。辛格尔顿班。

"AcheivementDictionary"类:只包含一个私有静态字典和访问方法。也是单分子级

问题:

成就是在第一个场景的"开始"时制作和添加的。添加它们后,我检索GameObjects并测试它们的标签是否有效,一切都很好。然后,从我的"levelManager"类中,我从下一个场景调用相同的方法,并得到一条消息,即gameObjects已销毁。如果我测试它们,它们也会返回null。

所以。。。我真正的问题是:

我如何使Acheivement游戏对象的即时副本在Unity中的所有场景更改中持续?

代码参考:

我不认为"acheivement"是有任何问题的类,如果你需要该类的代码,请大声说。

"AManager">

public class Amanager : MonoBehaviour {
public static Amanager acheivementManager = null;
public GameObject myAchObject;
void Awake () {
if (acheivementManager != null) { 
Destroy(gameObject);
} else {
acheivementManager = this;
GameObject.DontDestroyOnLoad(gameObject);
}
}
void Start () {
myAchObject.AddComponent<Acheivement>();

initAcheivements ("completeLevelThree", "Level 3", "Complete level 3","Level", 3,5);
initAcheivements ("completeLevelOne", "Level 1", "Complete level 1","Level", 1,5);
}
private void initAcheivements(string inAch, string inNavn, string inHow, string inCountType, int goalVal, int Reward) {

Vector3 loc = new Vector3 (0f, 0f,0f);
GameObject testAch;
testAch = Instantiate(myAchObject, loc, Quaternion.identity) as GameObject; 
//testLives.SetActive (true);
//testLives.gameObject.tag = "clone";
testAch.GetComponent<Acheivement>().setName(inNavn);
testAch.GetComponent<Acheivement>().setHowToGet(inHow);
testAch.GetComponent<Acheivement>().setCountType(inCountType);
testAch.GetComponent<Acheivement>().setGoalVal(goalVal);
testAch.GetComponent<Acheivement>().setReward(Reward);
testAch.tag = inCountType;
Debug.Log ("InitiAch did run");
AcheivementDictionary._Instance.achRegAdder(inNavn,testAch);
}

Achievement字典代码:

public class AcheivementDictionary : MonoBehaviour {
public static AcheivementDictionary _Instance;
private static Dictionary<string,GameObject> AchReg = new Dictionary<string,GameObject>();
void Awake () {
if (_Instance != null) { 
Destroy(gameObject);
} else {
_Instance = this;
GameObject.DontDestroyOnLoad(gameObject);
}
}
public void achRegAdder (string inName, GameObject theAch) {
AchReg.Add(inName,theAch);
Debug.Log ("achRegAdded did run");
readThisString = inName;
tryRead = true;
}
private void readIt() {
Debug.Log ("readItRan"); 
GameObject testShit; 
AchReg.TryGetValue("Level 1",out testShit);
Debug.Log("if it works level should stand there ---> " + testShit.tag);
tryRead = false;
}
public void achRegReader () {
readIt ();
}

运行时,控制台中会出现以下消息:

initAch确实运行了

achRegAdded确实运行

initAch确实运行了

achRegAdded确实运行

readItRan

如果它工作水平应该站在那里--->水平

readitRan

MissingReferenceException:类型为"GameObject"的对象已被销毁,但您仍在尝试访问它。您的脚本应该检查它是否为null,或者不应该销毁obejct。

这是用于在achRegReader失败时调用它的代码行(来自另一个类):AchievementDictionary_实例.achRegReader();

另外:如果我计算字典中对象的数量,它会返回2。他们只是被摧毁了,我真的不明白为什么。我的程序中没有任何代码可以破坏这些。

我的猜测是:

关于在Singelton类中放置静态字典,存在一些我不理解的问题。Acheivements的已安装副本不会在场景中持续存在,因为它们不是dontDestroyOnLoad,即使处理它们的两个类都是。不管怎样,在过去6个小时的斗争中,我都没有设法隔离或解决问题,请帮忙!

在initAcheivements函数中,

testAch.transform.setParent(transform);

DontDestroyOnLoad(testAch);

你的问题是由testAch不是不会被破坏的对象的子对象或者testAch本身不是不会被摧毁的对象引起的。

作为对您评论的回应,我很快写下了这段代码,它没有给我任何问题,我可能遗漏了什么吗?

Vector3 loc = new Vector3(0f, 0f, 0f);
var testAch = Instantiate(myAchObject, loc, Quaternion.identity) as GameObject;
DontDestroyOnLoad(testAch);

最新更新