• 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
1
Question by moghes · Sep 09, 2013 at 12:55 PM · spriteparent-childdynamicallytk2d

Add a child GameObject to a tk2d sprite dynamically

Hello everyone,

I have a 2D character with "Idle" animation, (3-4 frames) and I am using tk2d. When I hit my character, I need to add a scarf/injury (png) to the hit point.

I am able to Instantiate at hit point. I am able to make it child to the enemey character. But the problem is that the sprite moves, and its not the transform.position that changes, but the textures.. and as a result, the scarf stays at its place, where the character moves.

I can't figure out a method to stick my png to the hit point.. Maybe there's a way to change the texture atlas dynamically?

Sure I can have tons of millions of textures.. with a scarf at each position .. bla bla.. sure not thinking of that dumb way.. As I have many characters/levels..

Hope to get some hints.. thanks

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by DannyB · Sep 09, 2013 at 02:54 PM

Sounds like you have the correct solution, but you may be doing something wrong.

I suggest this:

  1. Create a scarf prefab (by using Create -> tk2d -> Sprite in the hierarchy and saving it as a prefab)

  2. Set your character script to have a public GameObject scarfPrototype object.

  3. Instantiate that prefab as needed: GameObject scarf = Instantiate( scarfPrototype, ...)

  4. Parent it: scarf.transform.parent = transform

There is no reason this should not work. If it doesn't please provide more info or code.

Comment
Add comment · Show 7 · 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 moghes · Sep 09, 2013 at 03:23 PM 0
Share

dude thanks for the answer, and that's all what I am doing.. It is parenting accuratly (no problem there) but the movement of the character is due to the sprite and not mesh movement. As I tried pausing the game, and moving the character by playing woth transform values, the scarf moves with it, but I need the scarf a way attached to the texture and not the mesh.. maybe couldn't explain as needed..

what code to post? all regular staff done like:

 tempScarf = Instantiate(scarf , hit.point , Quaternion.identity) as GameObject;
             tempScarf.transform.parent = currentCharacter.transform;



avatar image DannyB · Sep 09, 2013 at 03:27 PM 0
Share

Why are you moving the sprite and not its transform? Paste your line or two that moves the sprite?

avatar image moghes · Sep 09, 2013 at 04:21 PM 0
Share

not moving the sprite! Did move the transform manually from inspector while game running, just to check if scarf moves with character. The prefab stands in the middle of the screen, and the character has an idle movement (sprite) while mesh is static.

avatar image DannyB · Sep 09, 2013 at 04:37 PM 0
Share

You said "but the movement of the character is due to the sprite and not mesh movement" and "but I need the scarf a way attached to the texture and not the mesh"...

avatar image moghes · Sep 10, 2013 at 08:13 AM 0
Share

man I have 3 texture in my collection, as they change, you see the character at Idle move, but the mesh is in the same place. Not applying any code to move anything.

alt text

pic on left, character is leaning left, as you see when he is leaning to the right, the scarf still same place ..

check the Heirarchy as wellalt text

char.jpg (89.0 kB)
heirarchy.jpg (33.4 kB)
Show more comments

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

16 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

Related Questions

Create new sorting layer for sprite in code 0 Answers

Background sprite in TextMeshPro 0 Answers

Sprite editor and texture 1 Answer

dynamic prefabs in streamed scene asset bundle 1 Answer

import from photoshop as Sprites 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