Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Mechlordx · Jan 27, 2015 at 09:35 PM · objectresourcesmonobehaviourcast

Cast object to monobehaviour

I am trying to cast an object[] to monobehaviours, and even though I've copied the same logic used in the Resources.LoadAll documentation it still wont work. If I run the script this way, it says cannot cast source type to destination type. If I remove the "(MonoBehaviour)" cast it says "a cast exists, did you forget to use one?" or something like that.

  Object[] scripts = Resources.LoadAll ("BossScripts");
 
         pool = new MonoBehaviour[scripts.Length];
         for(int x=0;x<pool.Length;x++)
         {
         pool[x] = (MonoBehaviour) scripts[x];
     }



Comment
Add comment · Show 5
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 LessThanEpic · Jan 27, 2015 at 09:46 PM 0
Share

What are you trying to do with the BossScripts? You normally don't do a Resources.Load for a script and it's most likely not the right approach for what you're trying to accomplish.

avatar image Mechlordx · Jan 27, 2015 at 10:34 PM 0
Share

I'm dynamically loading and adding scripts because I don't want contributors to have to edit the prefab/scene in order to add something.

avatar image Glurth · Jan 27, 2015 at 10:55 PM 0
Share

do you load ANY items if you use this version of LoadAll() (from the docs):

 $$anonymous$$onoBehavior[] pool=Resources.LoadAll<$$anonymous$$onoBehavior>("BossScripts");
avatar image LessThanEpic · Jan 27, 2015 at 10:55 PM 0
Share

It sounds like gameObject.AddComponent() should fit your needs. That's the standard approach.

avatar image Mechlordx · Jan 27, 2015 at 11:01 PM 0
Share

@Glurth "The type or namespace "$$anonymous$$onoBehaviour" could not be found"

edit: No it "works" it was just a typo. However, it doesn't find anything. Is there another type that would detect C# scripts

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by MaxMallon · Sep 19, 2017 at 07:44 AM

I have a game controller that spawns the player, and adds a component to it based on choices made in the menu. The 'standard approach' of having it be linked to added components doesn't make any sense here. The game controller doesn't need any of the scripts the player gets added, and I can't just put them all on the player too, as they're all child-scripts of a parent type, and would do conflicting stuff. I made an array of scripts which could be added, and at first used MonoScript, but then it turned out that doesn't work in a build.. (makes me wonder why it's even there to begin with =__=) Now I'm trying with Objects, but it just won't cast it back into the right data-type to be able to add it onto the player. Cannot convert UnityEngine.Object into System.Type, and the likes is what I keep getting. Does any of you lot know how to solve this? I'm posting it here as this is the first post I've found of at least a similar problem after an hour of searching. @LessThanEpic @Glurth @Mechlordx

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

22 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

Related Questions

clientRpc array of System.object loss object type and can't cast the objects 0 Answers

move another object when 2 other objects collide 1 Answer

Cannot cast from source type to destination type 1 Answer

Cast Vector3 to object 1 Answer

How to call a function from one class to another class? 2 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