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 BlackWingsCorp · Feb 20, 2014 at 09:29 AM · c#enemydamagehealth

Enemyhealth component problem

Hey guys I'm writing a script that creates an array of all enemies then draws a ray from the player character, when the ray hits one it becomes the targeted enemy. But once I kill that enemy all the others get killed as well. Here's my scripts:

 using UnityEngine;
 using System.Collections;
 
 public class Attack : MonoBehaviour {
     
     public RaycastHit hit;
     public Vector3 directionRay;
     public int range = 20000;
     public GameObject player;
     public GameObject projectile;
     public GameObject[] targets;
     public static GameObject target;
     public float speed = 200.0f;
     bool isFired = SelfDest.isFired;
     bool isReloading;
     public Texture2D crosshair;
     public int zoom = 200;
     public int normal = 60;
     public float smooth = 40;
     public int bulletnumb = 12;
     public int magazine = 3;
     public AudioClip shoot;
     public AudioClip reload;
     public bool hashit = SelfDest.hashit;
 
 
     // Use this for initialization
     void Start () {
         player = GameObject.FindGameObjectWithTag("Player");
         isReloading = false;
         targets = GameObject.FindGameObjectsWithTag("Enemy");
     }
     
     // Update is called once per frame
     void Update () {
         directionRay = transform.TransformDirection(Vector3.forward);
         Debug.DrawRay(transform.position, directionRay * range, Color.red);
         if(Physics.Raycast(transform.position, directionRay, out hit, range)){
             if(hit.collider.gameObject.tag == "Enemy" && hashit == true){
             target = hit.collider.gameObject;
             }
         }
         if(Input.GetButton("Fire1") && bulletnumb >= 1){
             if(!isFired){
                 isFired = true;
                 GameObject bullet = Instantiate(projectile, transform.parent.position, transform.parent.rotation) as GameObject;
                 bullet.rigidbody.velocity = transform.forward * speed;
                 audio.PlayOneShot(shoot);
                 bulletnumb--;
                 Physics.IgnoreCollision(bullet.collider, player.collider);
                 EnemyHealth eh = (EnemyHealth)target.GetComponent("EnemyHealth");
                 eh.AdjustHealth(-30);
                 Invoke("Shoot", 0.45f);
             }
         }
             if(bulletnumb == 0){
                 Invoke("Reload1", 0.75f);
             }
         if(Input.GetButton("Fire2") && isReloading == false){
             isReloading = true;
             Invoke("Reload2", 0.75f);
         }
     }
 
     void Shoot(){
         isFired = false;
     }
     
     void Reload1(){
         if(bulletnumb <= 0 && magazine >= 1){
             bulletnumb = 12;
             audio.PlayOneShot(reload);
             magazine--;
         }
     }
     
     void Reload2(){
         if(magazine >= 1){
             bulletnumb = 12;
             audio.PlayOneShot(reload);
             magazine--;
             isReloading = false;
         }
     }
     
     
     void OnGUI(){
         GUI.Box(new Rect(Screen.width/2, Screen.height/2, 10, 10), crosshair);
         GUI.Box(new Rect(10, 35, 40, 20), bulletnumb + "/" + magazine);
     }
 
 }


Any help will be immensely appreciated. Thanks in advance

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 mattyman174 · Feb 20, 2014 at 09:44 AM 1
Share

Why is target a static member?

avatar image BlackWingsCorp · Feb 22, 2014 at 05:52 PM 0
Share

Sorry for the late reply. I use target in order to get the enemyhealth component in the bullet script. I found the error I'll post it. Thanks anyways

1 Reply

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

Answer by BlackWingsCorp · Feb 22, 2014 at 05:55 PM

Apparently the error was from the enemyhealth script I put the health int variable static. Anyways I hope the above script helps anyone trying to make a basic attack system.

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

19 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

Related Questions

Health below zero but shows negative numbers 1 Answer

Object Not Recieving Damage 3 Answers

One enemy triggers all the enemies 2 Answers

How i make the player damage the enemy specific enemy he is colliding with 1 Answer

How to create an NPC enemy which can take damage and be killed when its energy or health reaches zero? 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