简体   繁体   English

静态变量存储1类的值,但不显示在2类c#中?

[英]static variables stores values from 1 class but does not display in 2nd class c# unity?

i have 3 classes. 我有3个班级。

  1. static class in which i declared 2 game objects 我在其中声明了2个游戏对象的静态类

     public static Gameobject attacker; public static Gameobject defender; 
  2. attack combination in which i chose attacker and defender , in this scene it gets and display values fine , 我选择了攻击者和防御者的攻击组合,在这种情况下,它得到并显示了很好的值,

     staticclass.attacker = gameObject; Debug.Log("I am attacker" + staticclass.attacker); 

but it is not displaying values n 3rd class result class , any idea how to do that 但它不显示值n第三类结果类,任何想法如何做到这一点

  1. diceresult 选择

     Debug.Log(" ATTAKERRRRRRRRRRRR " + staticclass.attacker); 

dice result is in another scene 骰子结果在另一个场景中

When you make GameObject, Component or any script/component that derives from the Unity's Object static , it will still be destroyed/cleaned up when you load new scene. 当您使GameObject,Component或从Unity的Object派生的任何脚本/组件为static ,在加载新场景时仍将 销毁/清理它们

If you don't want this to happen that you must call DontDestroyOnLoad on all those Unity Object static variables immediately after initializing them. 如果您不希望发生这种情况,则必须在初始化所有Unity 对象 static变量后立即对其调用DontDestroyOnLoad

DontDestroyOnLoad(attacker);
DontDestroyOnLoad(defender);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM