#Heya guys, I'm having trouble with this

1 messages · Page 1 of 1 (latest)

vocal palm
#
using System.Collections.Generic;
using UnityEngine;

public class Invulnerability : MonoBehaviour
{
    Renderer rend;
    Color c;

    void Start()
    {
        rend = GetComponent<Renderer>();
        c = rend.material.color;
    }

    void OnTriggerEnter2D(Collider2D col)
    {
        if (col.gameObject.CompareTag("Enemy") && PlayerMovement.healthPoints > 0)
        {
            StartCoroutine("getInvincible");
            Debug.Log("I got invincible!");
        }

        IEnumerator getInvincible()
        {
            Physics2D.IgnoreLayerCollision(7, 8, true);
            c.a = 0.5f;
            rend.material.color = c;
            yield return new WaitForSeconds(3f);
            Physics2D.IgnoreLayerCollision(7, 8, false);
            c.a = 1f;
            rend.material.color = c;
        }
    }
}```
#

I keep getting a warning of "CS8321 - The local function 'getInvincible' is declared but never used Assembly-CSharp"

#

Even though I'm thinking, yes it is? It's right there being called by the coroutine

hazy bramble
#

A) Do not nest the coroutine
B) StartCoroutine(getInvincible());

vocal palm
#

So don't put it under a condition?

hazy bramble
#

no, you have your coroutine declaration nested within OnTriggerEnter2D

#

also your Debug.log is in the wrong place

vocal palm
#

Yeah, I only put it there just to see if the condition was met, or if the thing was processing, I was also following a tutorial so I wasn't sure about what happened

#

The OnTriggerEnter2D was for when the player character touches an enemy, then the player would be invulnerable for a couple seconds

#

Unless I'm getting the concept of this wrong and the tutorial is not helping

hazy bramble
#

when debugging triggers or collisions you need debug.logs as the first statement to show that the method is firing and after the condition is met.
For Coroutine you want debug.logs at the start and at the end

vocal palm
#

Gotcha

#

Okay, so I got the coroutine to work, I changed the OnTrigger to OnCollision

#

And that seemed to work, the invulnerability effect isn't really happening though

hazy bramble
#

in what way 'isn't really happening'?