Question by
Hugo_PH · Sep 18, 2021 at 07:58 AM ·
gameobjectscomponents
How to check if a game object has a component or other?
Hello, I have this code, I need to test if my "armaSeleccionada" object has the "controlArma" component (for firing projectiles) or the "RaycastFire" component (for raycast shooting) I don't know what to do, I'm a bit desperate :(
if (Input.GetButtonDown("Fire1"))
{
if (Armas[armaSeleccionada].GetComponent<ControlArma>() != null)
{
Armas[armaSeleccionada].GetComponent<ControlArma>().Disparar();
}
else if (Armas[armaSeleccionada].GetComponent<RaycastFire>() != null)
{
Armas[armaSeleccionada].GetComponent<RaycastFire>().isFiring = true;
if (Input.GetButtonUp("Fire1"))
Armas[armaSeleccionada].GetComponent<RaycastFire>().isFiring = false;
if (Armas[armaSeleccionada].GetComponent<RaycastFire>().isFiring)
{
Armas[armaSeleccionada].GetComponent<RaycastFire>().shotCounter -= Time.deltaTime;
if (Armas[armaSeleccionada].GetComponent<RaycastFire>().shotCounter <= 0)
{
Armas[armaSeleccionada].GetComponent<RaycastFire>().shotCounter= Armas[armaSeleccionada].GetComponent<RaycastFire>().rateOfFire;
Armas[armaSeleccionada].GetComponent<RaycastFire>().Disparar();
}
}
else
Armas[armaSeleccionada].GetComponent<RaycastFire>().shotCounter -= Time.deltaTime;
}
}
Comment
What exactly does not work? This is basically how you do it.
Your answer
Follow this Question
Related Questions
Make GameObject child of a child 1 Answer
How can i add a gameobject configured already on a unity application without rebuild this? 0 Answers
destroying instantiated groups of objects 1 Answer
Bad practice to create a GameObject for every BoxCollider2D? 0 Answers
Any way to create multiple empty GameObjects from a list? 0 Answers