Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Matchewman023 · Dec 23, 2017 at 02:32 AM · scripting problemraycast

How to access scripts in other game object with raycasts?

I'm trying to make a gun shoot and do damage to another object using raycast and the damage script is on the other object so i can remove health from that specific object, but I can't figure out how to access the script. This is what I have:

 using UnityEngine;
 
 public class Gun : MonoBehaviour {
 
     public float damage = 10f;
     public float range = 100f;
 
     //The Script Variable
     private MonoBehaviour enemyHealth = null;
     
     public Camera cameraObject;
 
     private void Update ()
     {
         if (Input.GetButtonDown("Fire1"))
         {
             Shoot();
         }
     }
 
     private void Shoot()
     {
         RaycastHit hit;
         if (Physics.Raycast(cameraObject.transform.position, cameraObject.transform.forward, out hit, range))
         {
             Debug.Log(hit.transform.name);
 
             //This is supposed to get the script from the object the Raycast hits
             //The error is at the <EnemyHealth>
             enemyHealth = hit.transform.GetComponent<EnemyHealth>();
 
             if (enemyHealth != null)
             {
                 //This is a public function from the other script
                 enemyHealth.Damage(damage);
             }
         }
     }
 }
 
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Larry-Dietz · Dec 23, 2017 at 02:50 AM

Exactly what error are you getting? Is your Debug.Log statement showing the correct name for the object hit? Or are you just getting a null return value?

Just taking a quick look at your script, the first thing that stands out is your declaration of EnemyHealth.

Even if the return value was not null, I would expect an error when you tried to access Damage, as MonoBehavior doesn't have a method called Damage.

Just a shot in the dark, I am assume your script is called EnemyDamage, so I would try changing your declaration to Private EnemyHealth enemyHealth; and see if that changes anything.

If not, let us know exactly what error you are seeing.

Hope this helps, -Larry

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 Matchewman023 · Dec 23, 2017 at 02:58 AM 0
Share

The debug statement does show the correct name and the error is: namespace 'EnemyHealth' could not be found. I tried your suggestion but it gave me the same error

This is my other script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class EnemyHealth : $$anonymous$$onoBehaviour {
 
     public float health = 100;
 
     public void Damage (float amount)
     {
         health -= amount;
 
         if (health <= 0)
         {
             Die();
         }
     }
 
     private void Die()
     {
         Destroy(gameObject);
     }
 }

avatar image Matchewman023 · Dec 23, 2017 at 03:02 AM 0
Share

This is the tutorial i'm using: https://www.youtube.com/watch?v=THnivyG0$$anonymous$$vo

but at the 7:30 mark where he does

 Target target = hit.transform.GetComponent<Target>();
 

is where i got errors so i tried the monobehaviour thing

avatar image
0

Answer by Matchewman023 · Dec 23, 2017 at 03:43 AM

I got it to work by taking the scripts out of my scripts folder and into the assets folder

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 Larry-Dietz · Dec 23, 2017 at 04:15 AM 0
Share

That is odd. Your scripts folder is within your assets folder, is it not?

avatar image Matchewman023 Larry-Dietz · Dec 23, 2017 at 04:18 AM 0
Share

it is in the assets folder

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

152 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 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 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 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 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 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 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

Spawn script on mesh crashes Unity 0 Answers

How to move the main camera in Google Cardboard? 0 Answers

UI Text showing weird string value? 1 Answer

Multiple Raycasts & Vector3.Reflects in a row. How? 1 Answer

How to get pixel coordinates when mouse click on Sprite Renderer 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