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 exploseis5 · Dec 08, 2016 at 06:47 AM · errornamespace

NameSpace Error EnemyHealth Cant Access Other Script.

Hey guys I'm fairly new to unity and was working on a project for school and came across this error

The Type or namespace name 'EnemyHealth' could not be found(are you missing a using directive or an assembly reference?)

All I'm trying to do is call the take damage function in my enemy health code. Here is my code:

using UnityEngine; using System.Collections;

public class PLayerShooting : MonoBehaviour { public ParticleSystem muzzleFlash; Animator anim; public GameObject impactPrefab; public float hitForce = 1000; int amount = 1;

 bool shooting = false;
 GameObject target;

 // Use this for initialization
 void Start ()
 {
     
     anim = GetComponentInChildren<Animator>();
 }
 
 // Update is called once per frame
 void Update ()
 {
     if(Input.GetButtonDown("Fire1"))
     {
         muzzleFlash.Play();
         anim.SetTrigger("Fire");
         Shooting();
     }
 }
 void Shooting()
 {
      RaycastHit hit;

      if (Physics.Raycast(transform.position, transform.forward, out hit, 50f))
      {
         EnemyHealth enemyHealth = hit.collider.GetComponent<EnemyHealth>();
         if (enemyHealth != null)
         {
             enemyHealth.TakeDamage(amount);
         }
         Instantiate(impactPrefab, hit.point, transform.rotation);
         impactPrefab.transform.position = hit.point;
         impactPrefab.GetComponentInChildren<ParticleSystem>().Play();         
      }
 }

}

Comment
Add comment · Show 2
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 JedBeryll · Dec 08, 2016 at 06:58 AM 0
Share

We need to take a look at the EnemyHealth script as well. (It's not inside a namespace is it?)

avatar image exploseis5 JedBeryll · Dec 08, 2016 at 04:34 PM 0
Share

$$anonymous$$y bad here is the EnemyHealth script. I don't believe it is in a namespace or at least i didn't intend for it to be

using UnityEngine; using System.Collections;

public class EnemyHealth : $$anonymous$$onoBehaviour { public int startingHealth = 1;
public int currentHealth; public int scoreValue; private GameController gameController;

 void Awake()
 {
     currentHealth = startingHealth;
 }

 void Start()
 {
     GameObject gameControllerObject = GameObject.FindWithTag("GameController");
     if (gameControllerObject != null)
     {
         gameController = gameControllerObject.GetComponent<GameController>();
     }
     if (gameController == null)
     {
         Debug.Log("Cannot find 'GameController' script");
     }
 }
 public void TakeDamage(int amount)
 {
     currentHealth -= amount;
     if (currentHealth <= 0)
     {
         Death();
     }
 }
 void Death()
 {
     gameController.AddScore(scoreValue);
     Destroy(gameObject);
 }

}

2 Replies

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

Answer by exploseis5 · Dec 08, 2016 at 05:50 PM

Hey guys i managed to solve the problem it was a matter of my scripts not being in the same script folder. I thank you JedBeryll for the quick response its good to see people interested in helping newcomers like me.

Comment
Add comment · 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
0

Answer by SoraMahiro · Dec 08, 2016 at 05:13 PM

Class is different from 'namespace', However, you can still do what you're trying to do. In PlayerShooting class do this: Add EnemyHealth enemyHealth; as a constant(meaning before any functions), then you should be fine. @exploseis5

Comment
Add comment · 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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Error The namespace '' already contains a definition for 'AAMode'. 1 Answer

Two assets using the same namespace..what to do? 1 Answer

The type or namespace name NetworkInformation does not exist in the namespace System.Net 1 Answer

System.Threading.Tasks 1' does not exist in the namespace 'System.Threading.Tasks' 1 Answer

A script by that name already exists... so what? 5 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