Following a video to create power ups, but its not picking them up and I dont know why.
Heres the code. I dont know whats wrong with it, from what I can tell its exactly the same as the video except that some of my scripts and variables were named differently. It should be giving a speed boost when the player touches it that lasts 10 seconds, but its not doing anything. I tried debugging it and it didnt even register me touching it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpeedBoostPowerUp : MonoBehaviour
{
public float SpeedBoost = 1.5f;
public GameObject pickupEffect;
//register touching power up void OnTriggerEnter2D(Collider2D other) { if (other.CompareTag("Player")) { StartCoroutine( Pickup(other) ); } } //pick up and activate power up IEnumerator Pickup(Collider2D player) { //Debug.Log("Powerup picked up!"); Instantiate(pickupEffect, transform.position, transform.rotation); PlayerMovement stats = player.GetComponent<PlayerMovement>(); stats.speed *= SpeedBoost; GetComponent<MeshRenderer>().enabled = false; GetComponent<Collider>().enabled = false; //last 10 seconds yield return new WaitForSeconds(10); stats.speed /= SpeedBoost; //destroy power up on pickup Destroy(gameObject); } }
//register touching power up void OnTriggerEnter2D(Collider2D other) { if (other.CompareTag("Player")) { StartCoroutine( Pickup(other) ); } } //pick up and activate power up IEnumerator Pickup(Collider2D player) { //Debug.Log("Powerup picked up!"); Instantiate(pickupEffect, transform.position, transform.rotation); PlayerMovement stats = player.GetComponent<PlayerMovement>(); stats.speed *= SpeedBoost; GetComponent<MeshRenderer>().enabled = false; GetComponent<Collider>().enabled = false; //last 10 seconds yield return new WaitForSeconds(10); stats.speed /= SpeedBoost; //destroy power up on pickup Destroy(gameObject); } }
0 Replies