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 Er_Mol · Dec 10, 2018 at 04:06 PM · prefabscript.2d-platformersprites

Getting last sprite from prefab.

I'm really new to Unity so this might be a very stupid question.

I have a ground spawning script that takes a GameObject array of prefabs, randomly chooses a prefab from that array and then instantiates that prefab in a position of a quad to which that script is attached to.

Problem is when I try to take the prefab that is selected to instantiate and take its last sprites position values and length to be able to move the quad in such way that the next prefab will be instantiated right next to the last one so the ground will look continuous.

I don't know if it's worth copying my code because it doesn't even compile, and It's just an amalgamation of a few solutions I found in similar topics.

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 Vega4Life · Dec 10, 2018 at 04:18 PM 0
Share

Let me get this straight. You are instantiating a prefab. On that prefab is a sprite. You are wanting to get that sprites last position so you can place it correctly? And you are currently trying to do that how?

avatar image Er_Mol · Dec 10, 2018 at 04:31 PM 0
Share

I have prefab that contains a couple of sprites(it's one sprite duplicated few times), and I want the position of that last sprite. I tried doing it by using GetComponents<Sprite>() but it's not a component so i tried using GetComponent<SpriteRenderer>().sprite, but that gets only one component and not renderer of every sprite in that prefab. So now I don't really know what to do.

1 Reply

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

Answer by Vega4Life · Dec 10, 2018 at 04:42 PM

Try using this line to gather all your sprites in the instantiated prefab.


         SpriteRenderer[] sprites = GetComponentsInChildren<SpriteRenderer>();
 

I think it would be better though to have a container script (named "SpriteContainer" for example) on the prefab. This keeps the references to the sprites, and you can reference the exact last sprite by itself if you wanted. This makes it super easy to access your sprites (or last sprite) when you instantiate the prefab. You instantiate the prefab, grab the sprite container script, then ask the container what the last sprite is.


Container idea:


 using UnityEngine;
 
 /// <summary>
 /// Contains this gameObjects sprites
 /// Attach to your ground object - drag the sprites into the component
 /// </summary>
 public class SpriteContainer : MonoBehaviour
 {
     [SerializeField] SpriteRenderer[] sprites;      // Link these by hand (dragging each sprite here) or do a GetComponent
     [SerializeField] SpriteRenderer lastSprite;     // Drag the last sprite here if you want to know position for placement
 
     public SpriteRenderer GetLastSprite { get { return lastSprite; } }
 
 
     //void Awake()
     //{
     //    // I wouldn't do this below.  Just showing you if you had to.  Direct references are better.
     //    // just drag the sprites into the component
 
 
     //    // Do this if you didn't do a direct reference on the component (which you should)
     //    //sprites = GetComponentsInChildren<SpriteRenderer>();
 
     //    // If you didn't direct reference for the last sprite,
     //    //then it could be the last sprite in the sprites array
     //    //but thats soley based on the order in the hierarchy - which means just do a direct reference
     //    //lastSprite = sprites[sprites.Length - 1];
     //}
 }



Then just grab this component from the gameObject you instantiated.


         SpriteContainer container = go.GetComponent<SpriteContainer>();
         SpriteRenderer lastSprite = container.GetLastSprite;


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 Er_Mol · Dec 10, 2018 at 04:53 PM 0
Share

I don't know if it's asking too much but could you show me some example on how to keep references to those sprites?

avatar image Vega4Life Er_Mol · Dec 10, 2018 at 04:55 PM 0
Share

Sure. Give me a few seconds.

avatar image Vega4Life Er_Mol · Dec 10, 2018 at 05:09 PM 0
Share

Put an example in the answer.

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

139 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 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

Trouble with tilemaps as prefabs for 2D plattformer 1 Answer

How to add sign interaction? 1 Answer

Can't add script behaviour while compiling ,Can't add script behavour while compling 1 Answer

I have a problem whit a script and prefabs 2 Answers

Trying to have my Editor Script save the changed settings when I hit Play 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