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 /
  • Help Room /
avatar image
1
Question by EthanCC19 · Jul 12, 2018 at 02:16 AM · scripting problemcounter

Need help with how my script needs to be attached

I have two game objects in my game and when one object is shot the counter information but when I have two objects in my game it increments their individual counters. I.E

alt text

I believe there is a problem on how I am attaching the script. I want one counter for all the gameobjects being shot. Here is my code.

 sing UnityEngine;
 using UnityEngine.UI;
 
 public class Target : MonoBehaviour {
 
     public float health = 10f;
     public int targetCounter;
     public int arraySize;
 
     public bool allTargetsHit = false;
     //public GameObject cube;
 
 
     private void Start()
     {
         
         
     }
 
     private void Update()
     {
         
     }
 
     public void takeDamage(float amount) {
         health -= amount;
 
         if (health <= 0) {
             //Die();
             changeColor();
             targetCounter++;
 
             //counter++;
             Debug.Log("Current targetCounter " + targetCounter);
         }
     }
 
     void Die() {
         Destroy(gameObject);
     }
 
     void changeColor() {
         gameObject.GetComponent<Renderer>().material.color = Color.red;
 
     }
 
     
 }

Any help is appreciated! Thank you <3

screen-shot-2018-07-11-at-70835-pm.png (22.8 kB)
Comment
Add comment · Show 3
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 tormentoarmagedoom · Jul 12, 2018 at 09:03 AM 0
Share

Good day.

I don't get exactly what is happening and what you need.

You have 2 objects, that can be shot (by the player or eachother??). And what are you counting? the impacts? impacts in each object or a global hot shots?

Do a good example, with some draw or screenshot explaining the process of what is happening, and another with the process you want to get, and we will try to help

Bye :D

avatar image EthanCC19 tormentoarmagedoom · Jul 13, 2018 at 04:53 PM 0
Share

@tormentoarmagedoom Here is a video better explaining my problem! https://www.youtube.com/watch?v=2mIbi$$anonymous$$U1mZY&feature=youtu.be

avatar image EthanCC19 tormentoarmagedoom · Jul 13, 2018 at 04:53 PM 0
Share

Yes! @tormentoarmagedoom I have 2 objects that when shot they change colors. I need a way to track when all game objects have been shot and have changed color. The counter is to serve for tracking the number of cubes shot. I have 2 cubes in the scene. When one cube is shot the counter increments by 1. When you shoot the second cube another counter variable is made and is incremented by 1(this is not what I want lol). I think it has something to do with my target script and how it is tracking each game object. Essentially I need a global counter for all the cubes in the scene being shot opposed to a single counter for each object.

2 Replies

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

Answer by tormentoarmagedoom · Jul 13, 2018 at 10:28 PM

Then you only need to create a special object, lika a "GameController" with a tag for eaxmple "Controller" with a script for example "GameRecords", with the targetcounter variable,

And make all cubes go look for that Object, get its script, and increase its variable. And only need to replace the

 targetCounter++;

for something like

 GameObject.FindObjectWithTag("Controller").GetComponent<GameRecords>().targetCounter++;

So, you find an object in the scene with the tag "Controller", acces it script called "GameRecords" and increased by 1 its variable.

As all cubes are acessing the same script, that variable will be increased every time a cube executes the code.

Bye!

Comment
Add comment · Show 1 · 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 Skelly1983 · Jul 13, 2018 at 10:47 PM 0
Share

This appears to complicate things, when going of the details in question the best option would be too simply make targetCounter static.

avatar image
1

Answer by Skelly1983 · Jul 13, 2018 at 05:45 PM

The targetCounter needs to be static, ie:

 private static int targetCounter;

This would make it specific to the type, instead of the object.

static (C# Reference)

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 EthanCC19 · Jul 15, 2018 at 10:44 PM 1
Share

Thank you this actually fixed my problem!

avatar image Skelly1983 EthanCC19 · Jul 16, 2018 at 07:41 PM 0
Share

Glad to hear this sorted your problem.

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

212 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

How can I add a toggle to this FPS counter script? 1 Answer

Enemy kill counter only counts first killed enemy 0 Answers

Please help me to solve this in unity C# scripts (TriggerCounter and OnTriggerEnter) 0 Answers

How to Make Text Count Objects in a Grid? 0 Answers

Checkpoint and Respawn 0 Answers


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