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 /
This question was closed Jun 26, 2013 at 07:37 PM by Jessy for the following reason:

Too subjective and argumentative. Simplicity is in the mind of the beholder. Forum topic.

avatar image
-1
Question by greatwhiteshark17283 · Jun 26, 2013 at 06:24 PM · transformarraytagsimplify

How can I simplify this script?

I made a script that detects if a certain object is near it every second and if so, it takes out health from the character and eventually they will re-spawn. I was wondering how I could make it so then it does the same with more than one tranform (so then it isn't just "enemy"). Is there any way I could do this with an array or a tag or something like that? This is the script:

 var distance;
 var enemy : Transform;
 var attackDistance = 2;
 var spawnPoint : Transform;
 var health = 5;
 var player : GameObject;
 private var maxHealth;
 
 function Start(){
     maxHealth = health;
     InvokeRepeating("Detection", 1, 1);
 }
 function Detection(){
     distance = Vector3.Distance(enemy.position, transform.position);
     if(distance < attackDistance){
         (health) = (health) - 1;
     }
     if(health == 0){
         Invoke("Respawn", 2);
         player.SetActive(false);
     }
 }
 function Respawn(){
     player.transform.position = spawnPoint.position;
     player.SetActive(true);
     health = maxHealth;
 }
Comment
Add comment · Show 6
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 KiraSensei · Jun 26, 2013 at 06:46 PM 0
Share

You could put a sphere collider on this game object, and when the player is is (with OnTriggerStay() method), you decrement its health. This will avoid to check every second if it is close enough. Sorry but I don't have the time to give you some code :)

avatar image SpecticalPro · Jun 26, 2013 at 07:09 PM 0
Share

The trigger stay method should work, and rather than using involkRepeating you should use a Coroutine.

avatar image robertbu · Jun 26, 2013 at 07:13 PM 1
Share

You can keep an array. If all your enemies are tagged, you could use GameObject.FindGameObjectsWithTag() either in Start() or every frame to get an array of game object and cycle through them. A better method would be to move the logic to the enemies. The enemies would detect when they are close to the player and then communicate to the player. See accessing other game objects.

avatar image greatwhiteshark17283 · Jun 26, 2013 at 07:16 PM 0
Share

The reason why I use InvokeRepeating is because there is because the enemy transform is following the character. If I used OnTriggerEnter, it would only do it once and the health would be 4 ins$$anonymous$$d of 5. By doing InvokeRepeating, as long as the enemy is close enough, it will take out 1 health every second. I am asking how I can make it work with more than 1 enemy.

avatar image robertbu · Jun 26, 2013 at 07:20 PM 0
Share

@kyleyo9 - he suggested OnTriggerStay() which will fire every frame the colliders are touching.

Show more comments

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

17 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

Related Questions

List and store vector3 of game objects with tag 1 Answer

Store Vector3 from Array of GameObjects to Vector3Array 1 Answer

modifing enemies target inside a radius 1 Answer

Argument is out of range in array 1 Answer

Using lists or arrays of transforms in Boo. 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