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 /
avatar image
0
Question by JoshPriceGames · Nov 08, 2018 at 02:06 AM · triggerscorescore systemadd

Add score when enemy is killed?

I have a gun script, that causes damage to the enemies. When their health is 0, the enemy is destroyed and a "dead" enemy (a ragdoll) is spawned in its place. The enemies are prefabs (so are the ragdolls), and are spawned in randomly. Its supposed to add 1 to a var in "ScoreKeeper.js" (on another object in the scene), and is connected to a thing that makes the score show on a GUI Text.

How can I make it so it adds 1 the the var "score" in "ScoreKeeper.js"?

Either right before the enemy is destroyed and replaced, or as soon as the ragdoll is spawned

Enemy Script:

 var health = 100;
 var AmountOfDamage = 10;
 var theLevel : String;
 var ragdoll : GameObject;
 var enemyprefab : GameObject;
 var relativePositionOffset : Vector3 = Vector3(0,0,0);
 var Script : ScoreKeeper;
 var CreateOneTime = true;
  
 
  function OnTriggerStay (myTrigger : Collider) {
 
          if(myTrigger.gameObject.tag == "Bullet"){
   health = health - AmountOfDamage;
   }
 
           if(health <= 0) {
   if(CreateOneTime)
   {
   Instantiate(ragdoll, transform.position + relativePositionOffset, Quaternion.identity);
   Destroy(gameObject);
   CreateOneTime = false;
           
           }
 
 }
 }


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
1

Answer by Drewtooroo · Nov 08, 2018 at 02:55 AM

I know of three solutions: static variables, singletons, or ScriptableObjects.


A static variable is a variable that is shared by all instances of a class. If a static variable is public, then it can easily be referenced by anything as ClassName.staticVar. So in ScoreKeeper.js you'd just need to make your "score" variable static and public:


 public static var score : int = 0;


It's quick and easy and works well. The usefulness of static variables can also be a downside, however, if you ever need multiple ScoreKeeper.js instances with their own "score" variables.


Unity has a tutorial on static variables.


A singleton is common design pattern that makes it easy to find an instance of your class, for classes that should only have one instance at any time. In a lot of ways it's similar to the static variable, except you're getting static instance of the class, which has it's own variable. A simple implementation might be:


 public class ScoreKeeper {
 
     public static var instance : ScoreKeeper;
 
     var score : int = 0;
 
     function ScoreKeeper() {
         if (instance == null) {
             instance = this;
         } else {
             Debug.LogError("There should only be one instance of this!");
         }
     }
 }


And then you could easily access the score variable using


 ScoreKeeper.instance.score


Singletons are great for all sorts of stuff, and if it works for you then you should go for it. Similar to static variables, it's possible to shoot yourself in the foot with singletons though. I encourage you to read more about singletons.


Last, but in my opinion the best and the most in Unity-style: ScriptableObjects. These are a class very similar to MonoBehavior (the same internally, I believe) except instead of attaching instances to GameObjects, the instances live as files in your assets folder. Components on your prefabs can all reference one of these ScriptableObject just by dragging and dropping in the inspector. Your ScoreKeeper would also reference the asset and update your UI when the score value changes (check out events if you don't want to evaluate the score every frame!).


 @CreateAssetMenu()
 public class Score extends ScriptableObject {
     
     public var total : int;
 }


ScriptableObjects are great because you can do all sorts of cool stuff with them once you get used to them. They are perfect for holding data you want to easily reference.


Unity has a nice tutorial on ScriptableObjects.


And here's the video that sold me on ScriptableObjects.

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 leftshoe18 · Nov 08, 2018 at 02:25 AM

You'll need a reference to the score keeper object and add one to the score when you kill the enemy. You can do this with FindObjectOfType<ScoreKeeper>().score += 1 or something similar.

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

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

116 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 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

Adding a collider to array 1 Answer

How to check if a car has perform stunt? 0 Answers

Score Display? 1 Answer

Storing high score 2 Answers

Reset score to 0 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