Hello again for the 100th time, I am trying to have my player be able to destroy/pass through obstacles in the game when he grabs a certain powerup. I have already made a script for it but it’s not working so something is wrong here. Been trying to figure out the problem all day. Can anyone please help me see what I’m missing in my script? Here’s my script:
public class PowerupScript : MonoBehaviour {
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player") {
ControllerScript playerScript = other.gameObject.GetComponent<ControllerScript> (); // not sure about the syntax here...
if (playerScript) {
// call function on playerScript to set player to invincible
playerScript.SetInvincible ();
// We speed up the player and then tell to stop after a few seconds
playerScript.coeffSpeedUp = 10.5f;
StartCoroutine (playerScript.StopSpeedUp ());
}
Destroy (gameObject); //This is to destroy the powerup
}
}
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "Dangerous") {
Destroy (col.gameObject); // This is to destroy the obstacles
}
}
}
And my Collision script that’s attached to the player if you need it:
public class Collision : MonoBehaviour {
ControllerScript player;
void Start()
{
player = GetComponent<ControllerScript>();
}
void OnCollisionEnter2D (Collision2D col)
{
// If the colliding gameobject is an Enemy...
if(col.gameObject.tag == "Dangerous")
{
// check if player is NOT invincible
if ( !player.isInvincible ) // this is the same as writing if(player.isInvincible == false)
{
// Find all of the colliders on the gameobject and set them all to be triggers.
Collider2D[] cols = GetComponents<Collider2D>();
foreach(Collider2D c in cols)
{
c.isTrigger = true;
}
// ... disable user Player Control script
GetComponent<ControllerScript>().enabled = false;
}
}
}
}