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 djizomdjinn · Feb 25, 2013 at 06:04 PM · addcomponent

How do I pass a script via inspector reference?

I have a function on a Character Controller that's called when it collides with any GameObject tagged ItemPickup, and calls the RequestPickUp method, passing the script, ItemPickup, from the colllided object.

    function RequestPickUp(itemScript : ItemPickup)
     {
         if (canPickUp) {
             itemScript.PickedUp();
             inventoryObject.AddComponent(itemScript.item);
         }
     }

Now, what I want to do is add the class itemScript.item is referencing to a holder GameObject (inventoryObject). I'd also like to be able to attach extensions from InventoryItem as well.

ItemPickup.js:

 var item : InventoryItem;
     function PickedUp() {
     Destroy(parent);
     }

InventoryItem.js is so far an empty class extending from MonoBehaviour. It's attached to the same object as ItemPickup.js and is linked via inspector reference.

 class InventoryItem extends MonoBehaviour
 {
 }

But when compile time rolls around... I get:

'No appropriate version of 'UnityEngine.GameObject.AddComponent' for the argument list '(InventoryItem)' was found.'

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 Montraydavis · Feb 25, 2013 at 07:25 PM

Well, I am not home to test out a C# script, but, here is the Javascript version of that code. Hopefully it won't be too hard to convert . . . .

 //File name to access is OOP.js
 
 var MyOOP : OOP ;
 
 function Start ( )
 {
    MyOOP = GetComponent ( OOP ) ;
    MyOOP.DoFunctionFromScript ( );
 }
 


Pretty simple script, and I hope this solves your issue. Good luck, and let me know if this is not what you are looking for . . . . .


Edit/Update:

//pickup

 var item : InventoryItem;
 function PickedUp() {
     Destroy(parent);
 }

 function Start ( )
 {
     item = GetComponent ( ItemInventory )
 }



If this is not working, please post the exact output from the console, please.

Comment
Add comment · Show 3 · 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 djizomdjinn · Feb 25, 2013 at 09:32 PM 0
Share

Well, I'm writing it in Javascript, so no problems on that account :D.

The main problem I have is that I'm planning on destroying the GameObject ItemPickup.js is attached to, and ItemPickup.js has a reference to a (customized or extended from) InventoryItem.js script, also attached to the GameObject.

What I'm hoping to do is to AddComponent the InventoryItem (or something extended from InventoryItem) that I'm pointing at onto inventoryObject... whereupon it complains that AddComponent(itemScript.item) doesn't work.

avatar image Montraydavis · Feb 26, 2013 at 01:39 AM 0
Share

Because you never accessed the component. Component for AddComponent needs to be referenced.

 item = GetComponent ( InventoryItem ) ;

Add to Start ( ); ( Or anywhere before item is added as component )

avatar image djizomdjinn · Feb 26, 2013 at 03:00 AM 0
Share

Nope, still getting the same compile error:

BCE0023: No appropriate version of 'UnityEngine.GameObject.AddComponent' for the argument list '(InventoryItem)' was found.

Though I'm pretty sure it wouldn't matter whether that line's in Start(), since I assigned it via Inspector anyways, and the compile error shows it's definitely being passed on.

What REALLY annoys me is that changing the line to:

 inventoryObject.AddComponent(InventoryItem);

works fine... but it's not what I want to do, since I'll need to add subclasses of InventoryItem, not just InventoryItem. Is there no clean way to clone components?

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

10 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Adding Prefab Components 1 Answer

Adding a component in the editor with a script 0 Answers

Destroying Object when Adding Component 2 Answers

Creating Hinge Joint at runtime? 1 Answer


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