- Home /
How do I make certain variables within the same script unique to the GameObject they're attached to?
Before we start: The '|' should be ignored as they are put there in order to breakup the text and make it more readable. (It's silly that it's somehow filtered out.) Now onto my question: |
||
| My goal is to write one script that I can use on different game objects and it should have specific variables tied to it on that game object only without affecting other scripts in the process. |
||
| For example, if I take this script and put it on two game objects each game object should have their own unique variable value in that same script. |
||
| If my question is not clear enough, I'm more than happy to elaborate further. |
||
| I have a good understanding of the unity editor, however, I'm pretty new to C# so I don't think it's unreasonable that I made a rookie mistake somewhere in my code. Anyways, the way I've got things setup is that I have two separate scripts. One script controls the values like the Team#, Health, Attack Damage, Cool Down, Cooling down, and Snap [This should be obvious if you watched infinity war hehe]. The other script controls the detection of a trigger being activated as a result of an enemy entering the trigger radius. The problem I'm currently having lies in the first script. It should also be noted that an empty attached to each game object in question contains both of these scripts and are marked as "Troop". |
||
| THIS IS THE SCRIPT THAT IS GIVING ME TROUBLE AS FAR AS I'M AWARE (Handles what to do when things are detected):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TrigDetect : MonoBehaviour
{
//public GameObject[] Enemy;
bool once = false;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Troop"))
{
//Debug.Log("Entered");
}
}
void OnTriggerExit(Collider other)
{
if (other.CompareTag("Troop"))
{
//Debug.Log("Exitted");
}
}
void OnTriggerStay(Collider other)
{
if (other.CompareTag("Troop"))
{
Fighting self = GetComponent<Fighting>();
GameObject g = GameObject.Find("Detection");
Fighting fScript = g.GetComponent<Fighting>();
//Enemy = GameObject.FindGameObjectsWithTag("Troop");
//Debug.Log("Staying");
//Debug.Log(Enemy);
//Debug.Log(self.Health);
//Debug.Log(fScript.Health);
if (once == false)
{
Debug.Log("I am the team:" + self.Team);
Debug.Log("I have detected the team:" + fScript.Team);
once = true;
}
if (self.Team != fScript.Team)
{
if (self.CoolingDown == false)
{
self.CoolingDown = true;
fScript.Health -= self.AttackDamage;
}
else
{
self.CoolDown -= Time.deltaTime;
if (self.CoolDown <= 0)
{
self.CoolingDown = false;
self.CoolDown = self.original;
}
}
}
}
}
}
THIS IS THE SCRIPT THAT IS NOT GIVING ME TROUBLE AS FAR AS I'M AWARE (Handles values associated with the script above):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Fighting : MonoBehaviour {
public int Team = 1;
public int Health = 100;
public int AttackDamage = 10;
public float CoolDown = 2;
public float original = 2;
public bool CoolingDown = false;
public bool Snap = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Snap == true || Health <= 0)
{
//Destroy(gameObject, .5f);
Destroy(transform.parent.gameObject);
}
if (Input.GetKey(KeyCode.N)) Instantiate(transform.parent.gameObject);
}
}
The expected result when I move one game object into the trigger radius of the other is that they should both start subtracting Health from each other based on the AttackDamage value. They should do this every time the CoolingDown value is false. When an attack is executed, it's flipped to true and a timer starts, when the timer is done it's flipped back to false. |
||
| However, upon moving the two objects into each other's radius', the first object has their health taken away as expected and then proceeds to do nothing until it's health reaches 0 then it dies because of the object attacking it. The object attacking is successfully attacking the other object but, is still not being affected by the object it's attacking.
Your answer

Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
How to change fall delay of a gameobject by time? 2 Answers
Colliding with the same object thousands of times per second 1 Answer