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 CornDog · Jun 18, 2015 at 03:56 PM · getcomponentruntimeinheritanceassembly

How do I access a script component added to a gameobject from an assembly at run-time

In my Unity project, I'm creating a game object and then adding a script to it at run time. The script (called Part in this case) is being added from an assembly. which ends up looking like this in the inspector: alt text Which is not quite the appearance I'm used to seeing on a script component in the inspector. (notice the Unity logo icon.) The script does function as expected though so that part works. The PROBLEM is that when I attempt to access the Part script component on this GameObject from a different script, using the following code:

 GameObject newPart = Instantiate(ContentManager.LoadPart(part), loc, Quaternion.Euler(rot)) as GameObject;
 newPart.GetComponent<Part>().Load(N["Parts"][i]);

I receive the error shown in the screenshot indicating that "Part" is null.

My second concern is that using this method (adding scripts from assemblies) will prevent me from making a GetComponent call for a base class type and have it successfully access any scripts that inherit from the base class type requested.

So How do I access a script component added at runtime from an assembly? and can it work with inheritance as well?

untitled.png (136.4 kB)
Comment
Add comment · Show 12
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 rhbrr5hrfgdfgw · Jun 18, 2015 at 04:03 PM 0
Share

are you sure you are instantiating a real mesh(not a null)? and what is Content$$anonymous$$anger.LoadPart

avatar image CornDog · Jun 18, 2015 at 08:38 PM 0
Share

Content$$anonymous$$anager.LoadPart is my content management system. it effectively replaces the Resources.Load function. That function returns a GameObject which is just a prefab loaded from an AssetBundle with a "Part.cs" script added from c# assembly(.dll) using System.Reflection before it is returned by LoadPart().

As to my certainty that its being loaded. You can see the part GameObject in the Hierarchy as "core_deck_tile(Clone)" and it does appear in the scene as expected with all expected code behavior. (I just cropped that part out of the image.)

avatar image Baste · Jun 18, 2015 at 09:35 PM 0
Share

What you're seeing in the inspector the part script is how an added component looks when Unity doesn't know exactly what it is - the same thing happens if you have a private, inner class that inherits from Component, and add it as a component.

I haven't really tried getting those with a GetComponent. Try something like this to figure out what's going on:

 GameObject newPart = Instantiate(Content$$anonymous$$anager.LoadPart(part), loc, Quaternion.Euler(rot)) as GameObject;
 Component[] comps = newPart.GetComponents<Component>();

 foreach(var comp in components) {
     Debug.Log("Component: " + comp + " of type: " + comp.GetType());
 }

If your Part script doesn't show up, you'll have to be creative. If it does show up, I dunno, it might be that your collection N returns null for the index "Parts"? Is that a dict?

avatar image CornDog · Jun 18, 2015 at 09:38 PM 0
Share

N is a JSONNode from SimpleJSON which I have tested and it is returning the correct value.

I'll have a go at your suggestion to see what it returns and post it here.

avatar image Baste · Jun 19, 2015 at 02:43 PM 1
Share

Yes. Lord, don't include the same code twice!

The Part component of your newPart object is of the Part type defined in the main dll. The Part type you're checking for with GetComponent is the Part type defined in your local assembly.

So if you do the same loop over the components, but write this ins$$anonymous$$d:

 foreach(var comp in components) {
     Debug.Log("Component: " + comp + " of type: " + comp.GetType());
     Debug.Log("Component is Part: " + (comp is Part));
 }

You'll probably get this out:

 Component: core_deck_tile(Clone) (Part) of type: Part
 Component is Part: False

Now, it's completely fine to have the "core" code in a dll, and include that in your project. In fact, that's a common technique to reduce compilation times. Just don't also include it in the Assets folder, or you will get horrible crashes like this.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

23 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

Related Questions

An OS design issue: File types associated with their appropriate programs 1 Answer

C# Inheritance - Reference the INSTANCE of a variable, is it possible? 1 Answer

Generate Script in Editor Script and GetType() returns null. 3 Answers

Load Script in AssetBundle at Runtime 1 Answer

Loading assemblies on android 64 bit 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