Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by Jsosborn · Dec 08, 2015 at 11:43 AM · instantiateprefabdestroystart

Trouble destroying a instantiated Prefab?

Hey, so I have a game where two players battle each other by colliding. After each collision their health decreases a little and when it hits 0 then they will be destroyed. So the way I have it set up is my game manager script instantiates both players from a set of prefabs from the void start () function. Then the gamemanager also keeps track of health and when health = 0 destroys the player. I use a separate script on a child object in each player object that determines when to activate the function damage () which does the heavy lifting in the gamemanager script. So basically the player calls to the manager each time its hit.

The problem is that even though I have spent the last 5 hours researching it, the instantiated prefab wont be destroyed - the function its in is calling itself but its just not being destroyed. Im wondering if this is a more complex issue involving calling a public void function from another script thats also custom or maybe its a issue with instantiating objects from the start () function.

 void Start () {
 
         PlayerOne = (GameObject) Instantiate (Player1, Player1Spwn.transform.position, Quaternion.identity);
         PlayerTwo = (GameObject) Instantiate (Player2, Player2Spwn.transform.position, Quaternion.identity);
 
 
 
 
     }
 
     void Update () {
 
         if (Input.GetKeyDown (KeyCode.G)) {
             Debug.Log (health1);
         }
 
 
 
     }
 
 
     public void DamageP1 () {
 
 
             health1 = health1 - 10;
             Debug.Log (health1);
          if (health1 <= 0) {
             Destroy (PlayerOne);
             Debug.Log ("hey");
         }
 
     }
 
 
     public void DamageP2 () {
 
 
             health2 = health2 - 10;
             Debug.Log (health2);
      if (health2 <= 0) {
             Destroy (PlayerTwo);
             Debug.Log ("hey");
         }
 
     }
 }


Here is the call function thing in the child object:

 void OnTriggerEnter2D (Collider2D theOtherCollider) {
         if (theOtherCollider.gameObject.tag == "Player2") {
             script.DamageP2 ();
          
         }


Thanks in advance!!!

Comment
Add comment · Show 2
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 alebasco · Dec 09, 2015 at 09:37 PM 1
Share

Please post the entire script when asking for help - or at least all the relevant parts. (You're missing where your variables are declared, your class names, and where the "child object" gets access to "script") If you edit it to provide more info, I might be able to help.

Instantiating from Start is fine. Calling public functions on other objects is fine.

On a side note, your code design could probably use some work. Why do the "children" not manage their own health? Think in terms of OOP. What is the purpose of each class, what should it handle?

avatar image Aggrojag alebasco · Dec 09, 2015 at 10:04 PM 1
Share

Also, you have some outputs in your code. $$anonymous$$nowing how these behave would also help narrow down the issue. In particular if you are outputting health1||health2 upon collision.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by heyhujiao · Dec 13, 2015 at 06:31 AM

Well, you can try this -

 void OnTriggerEnter2D (Collider2D theOtherCollider) {
          if (theOtherCollider.gameObject.tag == "Player2") {
              script.DamageP2 ();
 if (gameObject.name == "Player2(Clone)") 
 {
 Destroy(gameObject);
 }

}

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

6 People are following this question.

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

Related Questions

Active Prefabs 1 Answer

Accessing children of an instance doesn't work every time 1 Answer

how to instantiate object with content prefab and not him self 2 Answers

Destroying Instantiated Prefab 1 Answer

How do I destroy a Instantiated UI image that is pushed on the Canvas? 2 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