- Home /
What is a target component?
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#.
Please, provide the full context, at least the link to the tutorial you are mentionning.
https://www.youtube.com/watch?v=THnivyG0$$anonymous$$vo
(Somewhere around the 5:30-6:00 $$anonymous$$ part is where it starts)
He wrote this enemy script:
public class health = 50f;
public void TakeDamage (float amount) {
health -= amount;
if (health <= 0)
{
Die();
}
void Die () {
Destroy(gameObject);
}
}
Then he did this to the shoot script:
//existing
public GameObject fpsCam;
public float amount = 100f;
void Shoot () {
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
Debug.Log(hit.transform.range);
Target target = hit.transform.GetComponent<Target>();
}
}
This is most likely an Editor script. ¨
https://docs.unity3d.com/ScriptReference/Editor-target.html
Target is $$anonymous$$onoBehaviour script to be affected by the Editor script.
The $$anonymous$$B does not know about the Editor but the Editor needs to get info about the $$anonymous$$B so it can change what is rendered.
It says to make a public Object target;
line in the variable section. Is that what I am supposed to do for the Target target = hit.transform.GetComponent<Target>();
Where would I put the public Object target;
line, would I make a separate enemy script?
can't say. I would guess if the tutorial says so then that is what you should do.
Answer by KittenSnipes · Dec 19, 2017 at 02:30 PM
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.
Your answer
Follow this Question
Related Questions
Animating key on AnimationCurve in real time 0 Answers
Where should I call Physics2D.Raycast ? 0 Answers
i have two errors ; expected,i got two errr 1 Answer
Door smoothly opens to the side - Problem 3 Answers
Help with OnTriggerEnter 1 Answer