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
1
Question by Tim-A · Jan 04, 2013 at 08:46 PM · raycasthealth

Static var health

I have a monster game where they chase you. On the raycast enter there health goes down by 5. Here is my script.

  if(Input.GetButton("Fire1")){
 
       var dir = transform.TransformDirection(Vector3.forward);
       dir.Normalize();
       var hit : RaycastHit;
 
 
       Debug.DrawRay(transform.position, dir * distance, Color.blue);
 
       print("You fired a ray");
 
       if(Physics.Raycast(transform.position, dir, hit, distance))
       {
          if(hit.collider.gameObject.tag == "Monster")
          {
            health.Health -= 5;
 
            print("You Hit Him");
          }
        } 
     }

the problem is all of the monsters have the health so if you kill one, they all die. What should I do to prevent this.

[EDIT] Here is my health script.

 public var Health : int = 100;
 
 function Update () {
 
 
 if(Health <= 0){
 animation.Play("die");
 Destroy(gameObject, .4);
 
 }
 }

Monster Move Script :

 var target : Transform;
 var moveSpeed : int; 
 var myTransform : Transform;
 var alive : boolean = true;
 var stop : boolean = true;
 var dead : boolean = false;
 
 function Update () {
     target = GameObject.FindWithTag("Player").transform;
     
     if(health.Health <= 0)
     {
         alive = false;
     }
 
         
         
         if(alive == true && (stop == false))
         {
             transform.LookAt(target);
                 myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
 
                     
         }    
             
             
             
             
             if(target){
                          
                var dist = Vector3.Distance(target.position, transform.position);
              
                if(dist >= 1.6){
                 stop = false;
                }
                 if(dist <= 1.5){
                 stop = true;
                }
                if(stop == true){
                
               animation.Play("attack");
                playerHealth.health -= 1;   
                 
                 }}if(stop == false && (alive == true)){
                 animation.Play("run");
                 
     }
     
    
  if(dist >= 2.4){
   stop = false;
  }
    
    if(alive == false){
    Cash.Cash += 100;
      Destroy(gameObject, .5);
    }
    
    if(alive == false){
    dead = true;   
    }
    
    }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
4

Answer by Piflik · Jan 04, 2013 at 08:56 PM

Don't use static variables unless you really need to (and know what you're doing)...there is only ever one instance of a static variable, because it belongs to the class itself, not to its members. Use standard public variables.

Comment
Add comment · Show 17 · 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 Tim-A · Jan 04, 2013 at 09:01 PM 0
Share

How would I do that?

avatar image Eric5h5 · Jan 04, 2013 at 09:06 PM 3
Share

@Tim A: http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

avatar image Piflik · Jan 04, 2013 at 09:08 PM 0
Share

This in your enemy script: (called Enemy.js for this example, use whatever your script is actually called)

 public var health : int = 10;    //or whatever health you want them to have

This in your player script:

 if(Physics.Raycast(transform.position, dir, hit, distance)) {
     if(hit.gameObject.tag == "$$anonymous$$onster") {
        
         hit.gameObject.GetComponent(Enemy).health -= 5;

         print("You Hit Him");
     }
 }
avatar image Tim-A · Jan 04, 2013 at 09:22 PM 0
Share

var otherScript: OtherScript = GetComponent(OtherScript); otherScript.DoSomething();

what does it mean by DoSomething? I'm confused.

avatar image Piflik · Jan 04, 2013 at 09:39 PM 0
Share

DoSomething() is simply an arbitrary method defined in the other script. You can't only access other scripts' variables, but also their methods, as long as they are public.

Show more comments

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

11 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

Related Questions

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

Enemy health script using raycast not destroying tagged object 1 Answer

Raycasting help, enemy damage 0 Answers

Dispenser Help 1 Answer

How can i make a ray cast take health from enemies 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