简体   繁体   中英

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

i have 3 classes.

  1. static class in which i declared 2 game objects

     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

  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.

If you don't want this to happen that you must call DontDestroyOnLoad on all those Unity Object static variables immediately after initializing them.

DontDestroyOnLoad(attacker);
DontDestroyOnLoad(defender);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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