- Home /
GetComponent won't work
using UnityEngine;
public class Projectile : MonoBehaviour { public GameObject explosionVFX; public Weapon weapon;
void Awake()
{
weapon = GameObject.Find("Thingy").GetComponent<Weapon>();
}
void Update()
{
}
void OnTriggerEnter(Collider other)
{
Debug.Log(other);
Destroy(gameObject);
Instantiate(explosionVFX, transform.position, transform.rotation);
if (other.GetComponent<EnemyHealth>() != null)
{
hasCollided = true;
other.GetComponent<EnemyHealth>().TakeDamage(damage);
}
}
}
public class Weapon : $$anonymous$$onobehaviour
{
// Can be accesed from other scripts.
public float damage = 10.0f;
}
public class Projectile : $$anonymous$$onobehaviour
{
private Weapon weapon;
//public float projectileDamage = 10;
private void Awake()
{
// Get the weapon however you see fit.
weapon = FindObjectOfType<Weapon>();
}
//Assume getting called from OnTrigger.
public void ApplyDamage(EnemyHealth health)
{
//If damage is a property of the weapon.
health.TakeDamage(weapon.damage);
// Or if damage is a property of the projectile.
//health.TakeDamage(projectileDamage);
}
}
Answer by k234234w · Dec 18, 2018 at 12:04 AM
Maybe move your Destroy(gameObject) call to the end of the OnTriggerEnter, or maybe put it right below the call to TakeDamage.
What is currently happening? Is your log from the OnTriggerEnter happening (is it actually going in the OnTriggerEnter). Can you add a log inside the if statement to see if it is actually entering the if statement.
I'm trying to get damage and a bool(hasCollided) from another script. If I change monoBehaviour to weapon it works but I can't use getcomponent or properties.
Your answer
Follow this Question
Related Questions
Why does my player not move? 1 Answer
How do I build up speed while wallrunning? 1 Answer
How Do I rotate a Object Based On the player's location? 1 Answer
How to tell if a Canvas UI slider is being clicked not over Value 2 Answers
How do I make the enemy script/AI stop following me when It is in view of the player camera? 1 Answer