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 Borzi · Oct 02, 2013 at 07:18 PM · collidergetcomponentontriggerenterhit

Collection Using "GetComponent"

I am trying to create a script that allows the player to collect power ups. When the player collides with this powerup, I want a variable on a script attached to the powerup to turn to true ("Collected"). The rest of the code works fine, but on the "GetComponent" line I get the error is "The best overloaded method match for "UnityEngine.GetComponent(System.Type)" has some invalid arguments". Anyone know what this means and how to fix it? Thank you in advance!

 void OnTriggerEnter (Collider hit)
         {
         if(hit.gameObject.tag == "Weapon")
         {
         if(Inventory.Instance.HCisUnlocked == false)
              {
              Inventory.Instance.HCisUnlocked = true;
              //Turn off the Collider
              hit.collider.GetComponent(TheScript).Collected = true;
              Debug.Log("Collected PowerUp");
              }
         if(Inventory.Instance.HCisUnlocked == true)
              {
              WeaponStates.Instance.HC.Ammo += AmmoBoost;
              hit.collider.GetComponent(TheScript).Collected = true;
              }
         }
         }




Comment
Add comment · Show 4
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 Stormizin · Oct 02, 2013 at 07:22 PM 1
Share

Can you please, edit this post with the error line?

avatar image clunk47 · Oct 02, 2013 at 07:27 PM 0
Share

Is Inventory a static class?? Please don't just ask "Why doesn't this code work?". You need to put some effort forth to give us the information we need in order to troubleshoot. I can't even test this given code because you haven't provided any of the scripts you are trying to access, or even the name of the class this code resides in. Please edit the question and be specific, we'll be happy to help.

avatar image Borzi · Oct 02, 2013 at 08:03 PM 1
Share

Okay so I edited it for you, sorry that I didn't make it clear at the beginning it was stupid of me for someone to just see what the problem is. I am just not used to using GetComponent so I wondered if someone could tell me how to use it correctly in this case. Thanks!

avatar image clunk47 · Oct 02, 2013 at 08:07 PM 0
Share

Thanks.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by clunk47 · Oct 02, 2013 at 08:09 PM

To use GetComponent in C# is a bit different than JS. For one, variables are private by default in C#, so you need to make Collected a public bool in your other script.

 public bool Collected;

To access it from your other script, a minor difference from what you had:

 hit.collider.GetComponent<TheScript>().Collected = true;
Comment
Add comment · Show 2 · 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 Borzi · Oct 02, 2013 at 08:17 PM 1
Share

I thank you! Good explanation i'll keep this in $$anonymous$$d next time ill do something with get component :)

avatar image clunk47 · Oct 02, 2013 at 08:17 PM 0
Share

Always glad to be of help :)

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

17 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

Related Questions

Access other collider without use of Trigger collider. 1 Answer

MonoBehaviour.OnTriggerX vs Collider.OnTriggerX 1 Answer

OnTriggerEnter not being called with multiple colliders 1 Answer

Problem with OnTriggerEnter 2 Answers

Best practice for checking which object has collided. 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