Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Internetman · Mar 15, 2016 at 11:18 AM · c#instantiatedestroyshield

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;
     
         }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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"));
          }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Internetman · Mar 15, 2016 at 01:27 PM 0
Share

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"

avatar image Ali-hatem Internetman · Mar 15, 2016 at 06:10 PM 0
Share

i have edited my answer

avatar image Internetman Ali-hatem · Mar 16, 2016 at 01:47 PM 0
Share

Works like a charm, thank you!

avatar image
0

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

  1. //Destroyobject(newShield); @Internetman

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

125 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges