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 SlyyGuyy · Mar 18, 2014 at 03:09 AM · javascriptplayerenemyscoring

Checking enemy death and adding score issue Javascript

I have two separate scripts for my Player and my Enemies...how can I call to the enemy script within the player script and check to see if a specific enemy has been destroyed in order to add to the player's score? I have 3 types of enemies in my enemy script and I want to set different numbers for when they are each destroyed.

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 MileSplit · Mar 18, 2014 at 03:12 AM

Unity has very good documentation on how to access other Gameobjects and scripts

link

If you scroll down to the bottom it explains how to access other scripts

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 Olgo · Mar 18, 2014 at 03:18 AM

Well, there are thousands of ways to do this. To give you the first method that comes to mind.

  • Create a public int in the Enemy script, call it pointValue or something.

  • Set the pointValue on each enemy to its prospective value.

  • Create a public int in the Player script, call it currentPoints or something.

  • When the Enemy's health is lower than zero, have it modify's the Player script, and add its point value to the Player's currentPoints.

Here's a quick script example...

 public int health = 100;
 public int pointValue = 10;
 public bool isDead = false;
 
 void Update(){
  if( health <= 0 && !isDead ){
    Death();
  }
 }
 
 void Death(){
  int playersPoints = GameObject.FindGameObjectWithTag("Player").currentPoints;
  playersPoints += pointValue;
  isDead = true;
 }


EDIT: sorry, i forgot we were talking Javascript. but that same logic would work anywhere. let me know if you still have troubles.

Comment
Add comment · Show 3 · 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 SlyyGuyy · Mar 18, 2014 at 03:38 AM 0
Share

I like this method and I understand it well..however...I believe your answer is in C#? I was curious as to how to do the above in Javascript

avatar image Olgo · Mar 18, 2014 at 02:44 PM 0
Share

yup, its in c#... I don't know javascript well enough to just switch it over. with a little bit of googling though... give me a $$anonymous$$ute

avatar image Olgo · Mar 18, 2014 at 02:54 PM 0
Share

I'm at work so I can't test it out, that should work. If you know the first thing about javascript, any compiler errors should be easily fixable.

     public var health : int = 100;
     public var pointValue : int = 10;
     public var isDead : bool = false;
      
     function Update(){
     if( health <= 0 && !isDead ){
     Death();
     }
     }
      
     function Death(){
     var playersPoints : int = GameObject.FindWithTag("Player").currentPoints;
     playersPoints += pointValue;
     isDead = true;
     }

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

22 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

Related Questions

Throw the Player from the Enemy away 1 Answer

Decreasing Player Health On Collision with Enemy 2 Answers

Enemy detection in the light(Javascript) 1 Answer

Enemy Shoots At Player When A Certain Distance 1 Answer

I use this script, but the enemy lose health if i don´t target him. 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