Home > database >  GameObject does not contain a def for TryGetComponet? Unity
GameObject does not contain a def for TryGetComponet? Unity

Time:07-20

Started using unity 3 days ago, trying to make a VR game for fun.

At this point in my project, I have a kunai that you can throw, I want it to do damage to a cube I created as a my target. I want the cube to be destroyed once the Kunai hits it.

I followed a tutorial and have these two scripts.

Kunai Script:

{
private void OnCollisionEnter(Collision collision)
{
    if(collision.gameObject.TryGetComponet<Enemy>(out Enemy enemyComponet))
    {
        enemyComponet.TakeDamage(3);
    }
    Destroy(gameObject);
}

}

Box/Enemy Script:

{
[SerializeField] float health, maxHealth = 3f;

private void Start()
{
    health = maxHealth;

}

public void TakeDamage(float damageAmount)
{
    health -= damageAmount;

    if(health <= 0)
    {
        Destroy(gameObject);
    }
}

}

I am getting a CS1061 Error, Kinda confused on what its trying to say to me though.

CodePudding user response:

TryGetComponent is mispelled in the Kunai script (the second "n" is missing). Also, I guess "EnemyComponet" is mispelled too ;)

  • Related