Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by jmailloux11 · Jul 21, 2020 at 02:09 PM · instantiatemonobehaviouraddcomponent

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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
Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jmailloux11 · Jul 21, 2020 at 06:46 PM 0
Share

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.

avatar image andrew-lukasik jmailloux11 · Jul 21, 2020 at 08:16 PM 0
Share

This sounds like you need some kind of permutation of factory pattern

avatar image jmailloux11 · Jul 21, 2020 at 06:51 PM 0
Share

$$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.

avatar image andrew-lukasik jmailloux11 · Jul 21, 2020 at 08:17 PM 0
Share

Storing references to script assets is of no use as those are just text source files

avatar image andrew-lukasik jmailloux11 · Jul 21, 2020 at 08:22 PM 0
Share

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.

avatar image andrew-lukasik · Jul 21, 2020 at 09:11 PM 0
Share

@jmailloux11 Above I added an example of how you can create a list of scripts

avatar image jmailloux11 · Jul 21, 2020 at 09:56 PM 0
Share

Can I debug if any names are invalid?

Also thanks for all your helpfull answers!

avatar image andrew-lukasik jmailloux11 · Jul 21, 2020 at 10:57 PM 0
Share

Yes. See OnValidate method I just added

avatar image jmailloux11 · Jul 21, 2020 at 10:08 PM 0
Share

Thanks a lot you've just made my day, I way stuck on that for way to long.

Your Solution works perfectly!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

159 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Instantiating an entire object at runtime 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges