• 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 monoblue · May 01, 2014 at 03:27 AM · instantiateinstantiation

Issue with instantiating parts of a characters body - 2D

I'm trying to create a situation where my character destroys an enemy and gains back a piece of his tail in the process. The tail is just a series of circle shaped sprites and they should instantiate in a straight line behind the character piece by piece. A good example is the old nokia game Snake. The pieces should continue to instantiate as long as the character destroys enemies. It should look like so:

Like so

I've set it up so that there is an empty game object attached to the character with a script on it that instantiates a tail piece prefab. The prefab contains the tail piece sprite with another empty game object attached to it with the same script, so that the tail continues to grow.

This is the method in the script that instantiates a tail piece. I've tested it outside of my game in a test scene and it works but instantiates infinitely:

 public void instantiateTail()
 {        
     clone = (GameObject)Instantiate (obj,      
     transform.GetChild(0).transform.position,   
     Quaternion.identity);
 
     clone.transform.parent = transform.GetChild(0).transform;
 
 }

Then I have an "enemyScript" that handles what happens when an enemy is destroyed:

 void OnTriggerEnter2D(Collider2D other)
     {
     if (other.tag == "Player") {
             hud = GameObject.Find ("Main                      
             Camera").GetComponent<HUDScript> ();                              
             hud.IncreaseScore (10);
             //add tail piece to character somehow//
         Destroy (this.gameObject);//destroys enemy
     } else {
     Destroy (this.gameObject, 5f);
     }
 
     }

So the player collides with the enemy, the enemy is destroyed and the player's score goes up. And then some code that handles the instantiation of the tail piece prefab. My issue is that I can't wrap my head around what this code should be. I tried setting up a boolean "dead" that would become true when the enemy was destroyed and trigger the instantiation script but no dice. I'm now thinking the pieces should instantiate within an array...? And I'm also having trouble figuring out how to make it so that one piece instantiates each time an enemy is destroyed. Any help would be much appreciated.

example.png (9.3 kB)
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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

Need help understanding scripted prefab behavior - when I click one, the script runs on ALL prefab instances, not just the one I clicked. 3 Answers

Simple Instantiate gameObject problem 3 Answers

How to instantly move a newly instantiated prefab? 2 Answers

GameObject instantiation in C# static constructor 2 Answers

Inconsistencies in Instantiation Code 1 Answer


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