Destroy attached shield to player (C#)

Hello! I have a shield gameobject that instantiate and sets it to parent on a player gameobject. Once the player activates a power up the shield instanciate, i have written in the code that when the player gets the shield, the player tag is changed to “Shield” tag, so that the enemies cannot damage the player. The shield is actived for 5 seconds and then the tag changes back to “Player”.

So here’s the problem, how can I destroy the shield on the player? When I instantiate the shield I call it newShield, so I want to destroy this object. But the problem is that this is another function… I’m greatful for all the help I can get!

Code for Picking up the shield:

using UnityEngine;
using System.Collections;

public class PickUpScript : MonoBehaviour 
{
	//Timer
	public float shieldTimer = 5.0f;

	//Bool
	public bool shieldPowerUpActivated = false;

	//GameObject
	public GameObject shield;
    
	void Update()
	{


		//If shield power up is active
		if(shieldPowerUpActivated == true)
		{
			shieldTimer -= Time.deltaTime;
			transform.gameObject.tag = "Shield";
		}

		if(shieldTimer <= 0.0f)
		{
			transform.gameObject.tag = "Player";
			//DestroyObject(newShield);
		}

	}
	
	void OnTriggerEnter2D(Collider2D col)
	{

		if(col.gameObject.tag == "PowerUp1")
		{
			SpawnShield();
			Destroy(GameObject.FindGameObjectWithTag("PowerUp1"));
		}
	}
}

And here’s the spawn shield function:

public void SpawnShield()
    	{
    
    		// Get the transform ahead of time for readability
    		Transform charTransform = GetComponent<MoveScript>().character.transform;
    		
    		// Instantiate and keep track of the new instance
    		GameObject newShield = Instantiate(shield, charTransform.position, charTransform.rotation) as GameObject;
    		
    		// Set parent
    		newShield.transform.SetParent(charTransform);
    
    		shieldPowerUpActivated = true;
    
    	}

i think this is more simpler :

 public GameObject shield;
 public float shieldTimer = 5.0f;

 void OnTriggerEnter2D(Collider2D col)
 {
     if(col.gameObject.tag == "PowerUp1")
     {
         Destroy(col.gameObject);
         StartCoroutine(SpawnShield());
     }
 }
 IEnumerator SpawnShield()
         {
             Transform charTransform = GetComponent<MoveScript>().character.transform;
             GameObject newShield = Instantiate(shield, charTransform.position, charTransform.rotation) as GameObject;
             newShield.transform.SetParent(charTransform);

             transform.gameObject.tag = "Shield";
             yield return new WaitForSeconds(shieldTimer );
             transform.gameObject.tag = "Player";
             Destroy(GameObject.FindGameObjectWithTag("sheldTag"));
         }

Hi. In your original script, line 29 (if statement ShieldTimer), you have two forward slashes before the line of code

  1. //Destroyobject(newShield); @Internetman