Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Ak-striker · Jan 15, 2019 at 07:40 AM · aistatic variable

Tracking Kills

the way my game works is that when the player enters the scene the enemies are spawned in. So i wanted to create a system that tracks player kills, and although this would be easy on its own i want to design it in a way that it passes through scenes and appears on a separate script. my initial thought was to add 1 to a static variable every time ai.health = 0 however because enemies are spawned in am not sure how to make that static variable accessible on another script on another game object.

If you find this confusing i would be happy to further explain.

any feedback or help would be greatly appreciated.

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 xxmariofer · Jan 15, 2019 at 08:14 AM

You want the counter inside your player? you access that var using the class_name.var_name

if the class that contains the public static int numberOfKills is Player it would be:

Player.numberOfKills

Comment
Add comment · Show 2 · 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 Ak-striker · Jan 15, 2019 at 08:18 AM 0
Share

i tried that but it always gives me an error saying that the static variable cannot be accessed

avatar image xxmariofer Ak-striker · Jan 15, 2019 at 08:51 AM -1
Share

$$anonymous$$ake sure the var is public @Ak-striker

avatar image
0

Answer by surfuay · Jan 15, 2019 at 07:43 PM

a few things. Do you already have a place that updates the kills, if so you'll want to reference it. If not then do everything in a second script (completely new).

IE

public class KillsManager : Monobehaviour

{

    public static KillsManager kills;

    public int killcount = 0;       

    //this makes sure that it exists and that only one exists at a time
    public void Awake()
    {
            if(kills == null)
           {
                    kills = this;
           }
           else if(kills != this)
           {
                    Destroy(gameObject)
           }
           //THIS IS THE PART THAT MAKES SURE THE INFORMATION PERSISTS BETWEEN SCENES
           DontDestroyOnLoad(gameObject)
    }


     public void Update()

    {
               //this will cause it to change everytime you get a kill
               killcount = UIManager.uimanager.score (or whatever goes up by 1 when you get a kill)
                         //script.staticName.variable
      }

}

so now your kill count will follow you everywhere you go. anytime you go back into the game tho, your kill count will reset to 0 since the KillManager only reads what the player Kills are. So if you want that to override you'll need to make a method inside where ever you change the kill amount where playerkills = KillManager.kill.killcount

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

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

Smooth path finding for agents with turning circles. 0 Answers

My AI drops stuffs on death even when not supposed to 1 Answer

Field of View calculation 1 Answer

How Can I make an instance select a random enemy and move toward? 1 Answer

GameController script has 3 separate phases - Alert Caution and Normal. As soon as Alert is activated it switches to normal which then switches to Caution. How do I fix this? 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