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;
}
Answer by Ali-hatem · Mar 15, 2016 at 12:35 PM
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"));
}
Hm, yes that works the same, but the shield is still visible on the player after 5 seconds and is not removed. Any idéas on how to get it removed my friend? The Gameobject "Shield"
Answer by Afro Dragon · Mar 15, 2016 at 05:47 PM
Hi. In your original script, line 29 (if statement ShieldTimer), you have two forward slashes before the line of code
//Destroyobject(newShield); @Internetman
Your answer
Follow this Question
Related Questions
When instantiating 3 clones are created rather then 1 2 Answers
Instantiate and then destroy a prefab in a list. C# 1 Answer
How to attach camera to the instantiated gameobjects sequentially in a for loop using C# script? 0 Answers
Gameobject array 0 Answers
Trying to replace 2 objects. Destroying assets is not permitted to avoid data loss. 0 Answers