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 Voridian · Feb 24, 2014 at 06:29 AM · c#raycastshootinghealth

Why does the wrong enemy die with this raycast? C#

Okay well I don't really know how to phrase this but basically I have a Raycast script that says that if it hits something tagged "Enemy" then to take -25 of the health int. only problem is that no matter which enemy I shoot only the one I declared as a GameObject will destroy and I cant figure out how to use a prefab as a GameObject. On top of that while using a debug I found out that both the enemy objects health decreases when only one is shot... I hope somebody can see the issue.

Raycast Script:

 using UnityEngine;
 using System.Collections;
 
 public class WeaponRaycast : MonoBehaviour {
 
     public GameObject Player;
     float isRay = 0;
     public GameObject bulletDecal;
 
     public GameObject enemyObject;
     private Enemy enemy;
 
     void Awake () {
         enemy = enemyObject.GetComponent<Enemy>();
     }
     // Use this for initialization
     void Start () {
 
     }
     
     // Update is called once per frame
     void Update () {
         Debug.Log(enemy.health);
         RaycastHit hit;
         if(Input.GetButtonDown("Fire1")){
             if(Physics.Raycast(Player.transform.position, Player.transform.forward, out hit, 20f)){
                 if(PistolController.clipSize > 0 && PistolController.canFire == true){
                 if(hit.collider.gameObject.CompareTag("Enemy")){
                     isRay = 1;
                     enemy.health = enemy.health - 25;
                 }
                 else{
                     Instantiate(bulletDecal, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal));
                     isRay = 0;
                 }
             }
             }
         }
         Debug.DrawRay(transform.position, transform.forward, Color.green);
     }    
 }

Enemy script:

using UnityEngine; using System.Collections;

public class Enemy : MonoBehaviour {

 public int health = 100;

 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
     if(health <= 0){
         Destroy(gameObject);
 }

 }

}

Thanks in advance if you can work it out.

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

Answer by Jamora · Feb 24, 2014 at 06:38 AM

It seems you get a reference to only one enemy; the one that is on the same GameObject as the one you define in the inspector. (Line 14)

You need to change line 30 to actually use the Enemy script in the GameObject that was hit. Something like:

 hit.collider.gameObject.GetComponent< Enemy >.health -= 25;
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 Voridian · Feb 24, 2014 at 07:07 AM 0
Share

hey there, thanks for the reply but it comes up with a "expression denotes a 'method group', where a 'variable', 'value' or 'type' was expected." when i alter line 30 to what you suggested.

avatar image ahaykal · Feb 24, 2014 at 09:57 AM 2
Share

.GetComponent () <--

avatar image Voridian · Feb 24, 2014 at 10:05 AM 0
Share

thank you both VERY much i have been stuck on this for ages.

avatar image
0

Answer by black1ops22 · Mar 17, 2015 at 07:21 AM

Also, just for fun (im giving it away for freeeeee) i created a universal gun script with firerate, FX and more!

TEST IT OUT:

using UnityEngine; using System.Collections; //Bulletspawnpoint MUST be named Bulletspawnpoint public class Tryintoscriptpewpews : MonoBehaviour {

 public Rigidbody Bullet; // Bullet, Bulletspawnpoint, Automatic, isShooting,Bulletsound, Magsize, Ammo, firerate
 public GameObject Barrelflash;
 public GameObject Bulletholesmoke;
 public GameObject Bulletspawnpoint;
 public GameObject Bullethole;
 public GameObject Blood;
 public bool Automatic;
 public float Range = 100f;
 public float Firerate = 8f;
 public float Magsize = 30f;
 public float Amountofclips = 3f;
 public float Damage = 15f;
 private float Counter = 0f; 
 private bool isShooting;
 public AudioClip Bulletsound;
 RaycastHit hit;

// These are the customizable values. // Create a spawnpoint and place it infront of ze gun

 void Update(){
     Counter += Time.deltaTime;
     if(Automatic == true) {
         if(Input.GetKey (KeyCode.Mouse0) && Firerate > Counter){
             isShooting = true;
             if(isShooting == true){Fire();}
             }
         Counter = 0f;
         }
     else {
             if(Input.GetKeyDown (KeyCode.Mouse0)){
             isShooting = true;
             if(isShooting == true){Fire();}
         }
     } 
 }

 void Fire (){
         Instantiate (Bullet, GameObject.Find ("Bulletspawnpoint").transform.position, GameObject.Find ("Bulletspawnpoint").transform.rotation);
         Bullet.velocity = Vector3.forward * 8;
         AudioSource.PlayClipAtPoint (Bulletsound, transform.position);
     if (Physics.Raycast (GameObject.Find ("Bulletspawnpoint").transform.position, transform.forward, Range)) {
         if(hit.collider.gameObject.tag == "Player"){
             Instantiate(Blood, hit.point, hit.transform.rotation);
             hit.collider.gameObject.SendMessage("ApplyDamage", Damage);
         }
     }
 }

}

Its not done tho

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

22 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

Related Questions

How can i modify a float in another script in C#? 1 Answer

AI Shooting for Aircraft 1 Answer

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Limited ammo from Raycast? 1 Answer


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