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
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
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
@tormentoarmagedoom Here is a video better explaining my problem! https://www.youtube.com/watch?v=2mIbi$$anonymous$$U1mZY&feature=youtu.be
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.
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!
This appears to complicate things, when going of the details in question the best option would be too simply make targetCounter static.
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.