I am watching a Brackey’s tutorial and he wrote this line of code:
Target target = //then he checked if it had a target component
then he said something about “target components”. What are those and how do I add those? They don’t seem to exist in Unity C#.
@connorwforman
Ok the answer is right in your face. Nice try anyway. This script:
public float health = 50f;
public void TakeDamage(float amount)
{
health -= amount;
if (health <= 0)
{
Die();
}
}
void Die()
{
Destroy(gameObject);
}
Has a title above it. Looks like this:
public class Target : MonoBehavior
The name after class in Brackey’s videos is what he is calling because it is a script you add to all things that are damageable. So whatever your script’s name is will be what you put.
So say I have:
public class HealthManager : MonoBehavior {
public int health = 100;
public void changeHealth(int amount) {
health += amount;
}
}
In my other script I would reference it like:
public class Gun : MonoBehavior
{
public int damageAmount = -10;
HealthManager health;
void Shoot() {
//Shoot bullet
if (//Object is hit) {
health = hit.GetComponent<HealthManager>();
health.changeHealth(damageAmount);
}
}
}
So it is as simple as that. Hopefully this helps.