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 MrFlibble · Mar 01, 2014 at 11:48 PM · c#raycastgunenemieshealth

How can i make a ray cast take health from enemies

How can i make a ray cast take health from enemies So I have a raycast script and a health script. I've been chopping and changing it to try and get it to work but so far have been unsuccessful. I run into two main problems.

First of all there is a problem where it will only take health from one instance of a zombie and not the others (So i manage to "kill" one zombie and the rest are invincible)

Secondly I'm getting this error "MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object."

I'm getting this even when I do check to see if the object is null.

Below is the code I currently have

RayCast Script:

using UnityEngine; using System.Collections;

public class ShotGunAttack : MonoBehaviour {

 public GameObject target;
 public float power = 200;

 // Use this for initialization
 void Start () {

 }

 // Update is called once per frame
 void Update () {

    if (Input.GetMouseButtonDown (0)) {

      //Attack();
      Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
      RaycastHit hitInfo;

      if(Physics.Raycast(ray, out hitInfo, 25.0f))
      {

           hitInfo.rigidbody.AddForceAtPosition(ray.direction * power, hitInfo.point);


           if(hitInfo.rigidbody.tag == "Zombie" && hitInfo.rigidbody != null); {

           ZombieHealth zh = (ZombieHealth)target.GetComponent ("ZombieHealth");
           zh.AdjustHealth (-10);

           }



      }

    }


 }

} Health script:

using UnityEngine; using System.Collections;

public class ZombieHealth : MonoBehaviour {

 public int fullHealth = 100;
 public int health = 100;

 public float healthBar;

 // Use this for initialization
 void Start () {

    healthBar = Screen.width / 2;

 }

 // Update is called once per frame
 void Update () {

    AdjustHealth (0);

 }


 public void AdjustHealth(int adj) {

    health += adj;

    if (health <= 0) {

      health = 0;

      if (gameObject != null) {

      Destroy (gameObject);

      }

    }

    if (health > fullHealth) {

      health = fullHealth;

    }

} } I've been stuck on this for a couple of weeks so any and all help would be much 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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Mar 01, 2014 at 11:52 PM

You need to get your ZombieHealth from the object you hit, not from the 'target' (which is only initialized once). Change line 31 to:

 ZombieHealth zh = hit.collider.GetComponent<ZombieHealth>();

Note this line depends on any game object that is tagged 'Zombie' having a 'ZombieHealth' script.

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 black1ops22 · Mar 17, 2015 at 07:15 AM

Try this 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);
         }
     }
 }

}

i think you can code a health script for the zombies. BTW not all features of this script are done yet...

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

21 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

Related Questions

Using RayCast to do Continuous Shooting? 1 Answer

Unity c# Raycast axis stuck on one axis 0 Answers

Using raycast gun to take down life of enemy when shot 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 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