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 kangta · Jul 25, 2012 at 04:32 AM · raycast

Effect only one gameobject when multiply have the same script

Hi guys I'm running into a bit of a jam. I have a targeting system that works like I need how ever when I send messages it gets sent to all the gameobjects with that script I've pasted the scripts below and thanks for your help in advance.

  #pragma strict
 
 //Player Stats
 static var playerhealth : int = 100;
 static var Defense : float = 2;
 
 // Base Damage
 static var baseMin : int = 2;
 static var baseMax : int = 5;
 static var BaseDamage : int;
 
 // Gui Display of hp
 var playerHP: GUIText;
 var player : GameObject;
 
 //Crit Damage 
 static var DmgMin : int = 2;
 static var DmgMax : int = 2;
 static var CritDamage : int;
 
 //Crit Chance
 static var CritMin : int = 1;
 static var CritMax : int = 10; 
 static var CritChance :int;
 
 //Combat Variables's
 var HitAmmount : int;
 var playerhit : int;
 
 //Raycast Variable's
 var distance : float = 30;
 var target : GameObject;
 
 //Displays Current Target
 var LockedOn : GUIText;
 
 function Update () 
 {
 
  if(Input.GetKeyUp(KeyCode.F)) 
      {
      Target();
      }
      if(Input.GetKeyUp(KeyCode.G)) 
      {
      AttackOne();
      }
  
  var txt: String = "Health Points:" +playerhealth;
  playerHP.text = txt;
  
  if ( playerhealth <= 0 )
  playerhealth = 0;
 
 
 }
 function Target () 
 {
  print("locking on");
  
 
  var dir = transform.TransformDirection(Vector3.forward);
  var hit : RaycastHit;
  
  Physics.Raycast(transform.position, dir, hit, distance);
  
  print(hit.collider.gameObject);
 
  target = hit.collider.gameObject;
  
  var LockedName = hit.collider.gameObject.name;
  
  var txt2 : String = " Current Target" + " " +LockedName;
  LockedOn.text = txt2;
 }
 function AttackOne ()
 {
 
  print("Player Atk");
 
  CritChance = (Random.Range(CritMin,CritMax));
  //print(CritChance + " " +"CritChance");
 
  if ( CritChance == 1 )
  {
  BaseDamage = (Random.Range(baseMin,baseMax));
  //print(BaseDamage + " " +"BaseDamage");
  
  CritDamage = (Random.Range(DmgMin,DmgMax));
  //print(CritDamage  + " " +"CritDamage");
  
  HitAmmount = BaseDamage * CritDamage;
  
  target.GetComponent(Enemy).Combat(HitAmmount);
  
  //print(HitAmmount);
  print("Crit");
  }
  else 
  {
  
  BaseDamage = (Random.Range(baseMin,baseMax));
  //print(BaseDamage + " " +"BaseDamage");
  
  CritDamage = (Random.Range(DmgMin,DmgMax));
  //print(CritDamage  + " " +"CritDamage");
  
  
  HitAmmount = BaseDamage;
  //print(BaseDamage + " " +"BaseDamage");
  print("No Crit");
  
  target.GetComponent(Enemy).Combat(HitAmmount);
  }
 
 }

 and here is the enemy script
 
 
 #pragma strict
 
 //Enemy Health
 static var EnemyHealth : int = 100;
 // Gui Display of hp
 var EnemyHP: GUIText;
 
 function Update()
 {
 
  var txt: String = "Enemy Health Points:" +EnemyHealth;
  
  EnemyHP.text = txt;
  
  if ( EnemyHealth <= 0 )
  {
  EnemyHealth = 0;
  Destroy(this.gameObject);
  }
 
 }
 
 function Combat(dmg:int)
 {
  EnemyHealth = EnemyHealth - dmg;
  print("it works");
 }
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 Owen-Reynolds · Jul 25, 2012 at 04:52 AM 0
Share

What do mean when you "send messages" it goes to all? The F and G keys? Can you be more specific about the problem?

avatar image kangta · Jul 25, 2012 at 04:58 AM 0
Share

Owen thanks for your reply, however Seth has already point out my error and my code is working fine. but again thank you.

1 Reply

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

Answer by Seth-Bergman · Jul 25, 2012 at 04:39 AM

Since EnemyHealth is a static var, it stays the same across all enemies. If you take out the static, I think that's what you want... So each enemy has their own health.

Comment
Add comment · Show 4 · 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 kangta · Jul 25, 2012 at 04:45 AM 0
Share

I tried that it didn't work, but thank you

avatar image Seth-Bergman · Jul 25, 2012 at 04:51 AM 0
Share

Are you sure? Is that what the problem is, that all enemies are having the same health? Try changing an enemy's health via the inspector..

avatar image kangta · Jul 25, 2012 at 04:54 AM 0
Share

Im an idiot I was changing it in the wrong script. Thank you so much for your speedy help.

avatar image Seth-Bergman · Jul 25, 2012 at 04:56 AM 0
Share

glad to help!

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Disabling a lineRender or Ray when there is no target 1 Answer

This doormanager script from unity 3x Game Development Essentials is supposed to raycast the door open but it doesn't. Why not? Code now attached. 5 Answers

racing game raycast problem 2 Answers

Raycasting 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