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 /
  • Help Room /
avatar image
0
Question by shamji · Jun 04, 2016 at 10:08 PM · scripting problemenemyshootinghealth-deductionhealth

Why is this script not working?

the script is meant to take away Enemy health and eventually destroy the enemy, however it does not work, and instead, I would be randomly shhoting an enemy, (NOT where the Target is,) and it would loose health, which is not what i want. the script is here:using UnityEngine; using System.Collections; using UnityEngine.UI;

public class Enemyhealth : MonoBehaviour {

 public static int health = 1000;
 public GameObject Bot;
 public Slider healthBar;

 // Use this for initialization
 void Start () {

     InvokeRepeating("LooseHealth",1,1);
 }

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

     var    laser1 = GameObject.FindWithTag("laser1");
     var laser2 = GameObject.FindWithTag("laser2");
     var Enemy = GameObject.FindWithTag ("bot");


     if (laser1 == GameObject.FindWithTag("Enemy").transform)
         {
         health = health - 1000;
     }

     if (laser2 == GameObject.FindWithTag("Enemy").transform)
     {
         health = health - 1000;
     } 




     healthBar.value = health; 


     if (health <= 0) 
     {
         Destroy (Bot);
     }

 }

}

any help would be appreciated

Comment
Add comment · Show 3
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 Lewance · Jun 04, 2016 at 10:37 PM 0
Share

It's hard to say exactly what edits to make without knowing a few more details about your project. Is this script attached to each enemy instance or to a game manager?

In any case, you might find it useful to do the Space Shooter tutorial which covers how to do this sort of thing.

avatar image shamji Lewance · Jun 04, 2016 at 10:54 PM 0
Share

@Lewance it is basically a 3d game in which you are a first person character able to shoot lasers from your hands, almost like Iron $$anonymous$$an, and you are to shoot robots,(Spheres with guns on their heads,) until they die, and they are not really dying

avatar image Lewance shamji · Jun 04, 2016 at 11:33 PM 0
Share

okay but what is this script attached to? if you don't know how to answer that question then I really recommend doing the tutorial i linked above

2 Replies

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

Answer by Lewance · Jun 05, 2016 at 02:27 PM

Okay, if it's attached to the bot then you don't need to use: GameObject.FindWithTag("Enemy").transform That function will find an object from the heirarchy that's tagged as an enemy and if that enemy is being hit by a laser then the rest of your code will cause damage to be taken by this bot.

Instead you can just say "transform", you're already on the right game object! i.e.

 if (laser1.position == transform.position)
 {
     health -= 1000;
 }

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 shamji · Jun 05, 2016 at 10:46 AM

@Lewance its attached to the bot, cause its caused enemy health. i used a slider and my script corresponds with a slider.

Comment
Add comment · Show 1 · 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 shamji · Jun 05, 2016 at 10:52 AM 0
Share

@Lewance its attached to the bot, cause its caused enemy health. i used a slider and my script corresponds with a slider

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

67 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

Related Questions

Enemy character disappears as soon as i hit play 0 Answers

i try make enemy health slider or any uI just show the health of Enemy Over head 0 Answers

Damage not working properly 1 Answer

Enemy dies in editor mode but not in build 2 Answers

How to make health and damage script function correctly? 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