My code did not work during the challenge and I am looking for an explanation why

During the challenge we were asked to get the gravestone to wobble when being attacked by a attacker. We also did not want the stone to wobble when a projectile went through it. I was able to get the gravestone to wobble when both a projectile and a attacker attacked the gravestone through this code here.

    private Animator animator;
    private Attacker attacker;
    private Shooter projectile;

    void Start(){

        animator = GetComponent<Animator>();
        attacker = GameObject.FindObjectOfType<Attacker>();
        projectile = GameObject.FindObjectOfType<Shooter>();
    }

    private void OnTriggerStay2D(Collider2D collider){
        if(attacker){
            animator.SetTrigger("underAttack trigger");
        }
    }
}

Why would the projectile in this case cause my gravestone to move?

Can you wrap the code around the code syntax so we can see the FindObject generic parameter? It should look like this:

[code]
FindObjectOfType<typeof>();
[/code]

But regardless of that, the problem probably happens because you are addressing the attacker value within the start method, so once it was addressed it won’t return null until the enemy script is destroyed. The collision don’t change the value of the attacker variable, so when it collides with something, if the attacker addressed in the start method is still active, it will wobble regardless of whom was the one colliding with it. To fix it you could either unmark the collision between the player projectile and the tombstone or make an if(collider.GetComponent<Attacker>()!=null){animator.SetTrigger("underAttack trigger");} inside the OnTriggerStay

Hi Joao,

I do not understand what you are asking for in your first response. However, your second response seems to be on the mark. I have not messed with the code since your posting but I will be trying out your suggestion and declaring the variable in a different section of the code, or adding the code you listed towards the end of your comment. In both circumstances I did not search for the collider though I only searched for attacker. Surprised it still works then but I suppose looking for the attacker in general comes with the attackers collider as it is attached to the script that I found when I looked for attacker in the start method.

1 Like

I do not understand what you are asking for in your first response.

Hey Tim,

When you copy/paste your code into the forum, unless you mark/format it as code, it sometimes can be more difficult to read. Johnny has provided one method of formatting the code, by placing those tags before and after your code, I have linked to an alternative below.

I took the liberty of updating your post earlier to apply the formatting to make it more readable :slight_smile:


See also;

1 Like

It still works because you are looking for AN attacker in the start method. The statement within the if(attacker) has nothing to do with the actual attacker neither his collider, it could be literally anything else such as if(true) or if(playerExists), the OnTriggerStay2D will be called for any object that touches the trigger area as long as their collision is marked in the collision matrix.

What you are saying with your code above is:

“If something triggers me (anything with an rigidbody2d + collider2d that enters my area), and if the reference for the “Attacker” (could be any Gameobject with the script Attacker) that I have saved during the start method still exists, then I will wobble”

It isn’t checking if the object that collided with it is actually the attacker.

The solution that I’ve pointed is checking if the object that entered the trigger area (you declared it as “collider”) has a script of type “Attacker” (which only the enemies should have), and if it does have, it will wobble.

@Rob thanks for that :smiley:

1 Like

Thanks Rob and Joao I appreciate the feedback on this. I understand the concepts here a lot better now.

2 Likes

Nice, is it working as expected now?

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms