- Home /
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.
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?
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.
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;
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?
Your answer
Follow this Question
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