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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

[Solved] How to Dynamicaly assemble an Object in your Resource Folder 1 Answer

Instantiate(GameObject) VS Instantiate(Resource.Load(Object Path)) 1 Answer

resources.load (instantiate from this.transform) rather then the centre of the map? 1 Answer

Picture loaded from resource corrupted. 0 Answers

Instantiated Prefab using its transform and object on other scripts 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges