简体   繁体   中英

How to know if a class is instantiated

I use the following code to share variables and methods between scripts

Main script

private FileScript1 Script1;

public void Start () {
  Script1 = new FileScript1(this);
  Script2 = new FileScript2(this);
  Script3 = new FileScript3(this);
}

Other scripts

class FileScript1 // FileScript2, FileScript3
{
    private Target _target;
    public FileScript1(Target target)
   {
       _target = target;
   }

}

Now I would need to know if the scripts were instantiated before accessing them from Target script. For the moment I solved with a boolean

public void Start () {
  Script1 = new FileScript1(this);
  myFileScript1 = true; 
  Script2 = new FileScript2(this);
  Script3 = new FileScript3(this);
}

and before accessing the script, I verify this variable. I was wondering, is not there a cleaner way to do it? I looked in the forums but without luck.

Common check example:

Debug.Assert(objectName != null, "Class is NOT instantiated");

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