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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by mpeterson418 · Sep 09, 2014 at 05:52 AM · instantiateresourcesaddcomponentresources.loadall

Load a list of scripts from folder

Question

would like to load a list of scripts that all extend from one script from a folder within resources. Next, I want to spawn an object, and randomly assign it one of those scripts. Is this possible?

What I've tried

o far, I've found Resource.LoadAll(), and it has found all the scripts in my folder, however, I'm not sure where to go from there. It seems like all of scripts have the type of UnityEditor.MonoScript when I call script.GetType(), instead of the class that I have created, like MyScript. I thought I was able to add them to a list through casting (`list.Add(script as MyScript)`), but that is just adding null to the list.

For the second part, I know I have to create an instance of a prefab using Instantiate, and then attach the script using GameObject.AddComponent(). The problem I have facing here is that I don't know all the types I will be using (since all the scripts are read from a folder). So far, I have something like this:

 var prefab = Instantiate(obj, pos, rot) as MyObj; // obj is the prefab, MyObj is the type
 var scriptType = myScript.GetType(); // myScript is randomly chosen from the list generated before.
 prefab.gameObject.AddComponent(scriptType); // Should add the script to the object, which should do it's magic.

However, I have a warning when instantiating that The referenced script on this Behaviour is missing! since obj does not contain a script to make it a class of MyObj. Next, because my list is full of nulls, I cannot add any scripts, so I can't tell if I'm doing this part right or not.

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 mattssheep4 · Mar 28, 2020 at 12:16 PM

Should be doable, see this link: https://answers.unity.com/questions/1175014/add-all-scripts-in-a-folder-to-a-gameobject-at-run.html

Based on that thread I think you need to use .Class() instead of .GetType().

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Resources.LoadAll on Current Folder Only? 1 Answer

(Similar Prefabs) vs (Single Prefab + AddComponent) 2 Answers

Instantiate a prefab and add a script to it 1 Answer

GetType is Monoscript, how to implement correctly? store type in variable? 1 Answer

Clickable GameObjects within one another... 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