• 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
1
Question by Discorides · Aug 13, 2013 at 10:05 PM · accessing from any script

How do I use Raycast and GetComponent to run a function in a script attached to the hit object (in C#)?

I've been trying for days, but I cannot get my head around GetComponent or get it to work.

I have a player and some cubes for it to shoot at. In a 'Player' script I'm using raycast for the weapon, which was working well when I just wanted 'Destroy (hit.collider.gameObject);' But now I want to make the cubes explode. I've set up a function in my 'Cube' script called Explode();

What I can't work out is how to get my player script which has hitinfo on the targeted cube to make that cube run it's Explode function.

Currently my line of code looks like: hit.collider.gameObject.GetComponent.Explode(); But that, and all the variations I've tried on it have been giving compile errors.

Any help would be greatly appreciated.

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
2
Best Answer

Answer by robertbu · Aug 13, 2013 at 10:14 PM

Try this:

 hit.collider.gameObject.GetComponent<ScriptName>().Explode();

'ScriptName' is the name of your script that contains the Explode() method (no quotation marks). Note you want to be sure the object you hit contains a 'ScriptName' script. If not, you will get a null reference exception when you execute this line of code.

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 MisterBinny · Oct 21, 2015 at 08:36 PM 0
Share

Don't forget to make the method public in the target script. (easy to forget then think "Oh this answer didn't work."

public void my$$anonymous$$ethod() {

}

avatar image drjenkin · Apr 09, 2016 at 10:57 AM 0
Share

Is there a way to access hit.collider.gameobjectGetComponent<genericscriptname>()

in which the raycast hit just accesses information in any script that is attached the game object?

Where you could have several different scripts: enemy, oilcan, cat which each had their own Explode(); function that would trigger

avatar image inferno222 drjenkin · Mar 25, 2017 at 04:55 PM 0
Share

Probably late, but you can make a base script that has virtual/abstract function explode. So the others scripts enemy,oilcan, cat override this function :)

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 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

Related Questions

Trouble with calling a variable from another script 5 Answers

GameObject.name to call script name? 1 Answer

Global data structure accessible by any script? 1 Answer

Is it possible to acces variables from the unity prefabs such as the sparks and smoke 0 Answers

accesing variable from other script 4 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