- Home /
 
How do I get a CS script in assets to be a stored monobehaviour variable
Hi, I'm making a game about grid cells. They can be instanciated and destroyed at will be the player.
I didn't wanted to make prefabs and wanted to find an easier way of knowing what to instanciate. So I made it create a new gameobject with a sprite renderer wich sprite is stored in a list. It worked out well but now I realise that I also need them all to have their own behaviour to simplify the managing of all the cells.
I'm having trouble knowing how to store a monobehaviour that dosen't have an instance.
Can you guys help me out?
Answer by andrew-lukasik · Jul 21, 2020 at 03:19 PM
Do you mean how to create a non-MonoBehaviour class instances inside a MonoBehaviour?
 public class MyGridScript : MonoBehaviour
 {
     public List<CellState> cells = new List<CellState>();
 }
 
 [System.Serializable]
 public class CellState
 {
     public SpriteRenderer spriteRenderer = null;
     public bool alive = true;
     public void Kill ()
     {
         if( !this.alive ) return;
         this.spriteRenderer.enabled = false;
         this.alive = false;
     }
 }
 
               EDIT:
This is how you can serialize list of scripts as AssemblyQualifiedNames , and then convert them all to actual Components on GameObject
 [Tooltip("EXAMPLES: \"UnityEngine.MeshFilter, UnityEngine.CoreModule\" and \"MyNewScript, Assembly-CSharp\"")]
 [SerializeField] List<string> components = new List<string>(){ "UnityEngine.MeshFilter, UnityEngine.CoreModule" , "UnityEngine.MeshRenderer, UnityEngine.CoreModule" };
 
                void Start ()
 {
     foreach( var str in components )
     {
         gameObject.AddComponent( System.Type.GetType(str) );
     }
     // Debug.Log( typeof(MyClassName).AssemblyQualifiedName );// prints valid names you can use
 }
 
                #if UNITY_EDITOR
 void OnValidate ()
 {
     foreach( var str in components )
     {
         var t = System.Type.GetType(str);
         if( t==null )
             Debug.LogWarning($"\'{str}\' gives no type");
         else if ( !t.IsSubclassOf(typeof(Component)) )
             Debug.LogWarning($"\'{str}\' is not a Component");
     }
 }
 #endif
 
              No, I mean that each cell can have its own script, Wich will be given to it upon instanciating.
I would like to have a list for all the scripts needed upon instanciation.
Thanks for responding btw.
This sounds like you need some kind of permutation of factory pattern 
$$anonymous$$aybe I'm stupid and there is now way a script asset can be stored at a list, but if there is tell me. This would help a lot.
PS: I basically need a list for multiple different script asset so I'm using a "$$anonymous$$onobehaviour" list.
Storing references to script assets is of no use as those are just text source files 
System.Type can be used to represent scripts and can be stored in a list too: List<System.Type> - but those must be (de)serialized manually tho. 
@jmailloux11 Above I added an example of how you can create a list of scripts
Can I debug if any names are invalid?
Also thanks for all your helpfull answers!
Yes. See OnValidate method I just added 
Thanks a lot you've just made my day, I way stuck on that for way to long.
Your Solution works perfectly!
Your answer
 
             Follow this Question
Related Questions
Associate objects to a prefab 1 Answer
Threads for Instantiate 1 Answer
Can't add component because class doesnt exist 1 Answer
Using AddComponent to add a Sub Class using a String 4 Answers