Prefab sprite glitch,Prefab canvas glitch
Hi there, so i am making a mobile game (TowerBlox) style, but with hamburgers. And im having this weird glitch, so i have a spawn point on top of the screen that spawns a prebaf with random sprites(meat, cheese, tomatoes, bacon etc..) and for some reason when they hit the next prefab to stack some of the sprites get behind the previews sprite. but only fails in some combinations, for example if a egg sprite drops on top of a meat sprite it is all good, but the opposite it glitches. Check the screenshot to see whats going on https://ibb.co/txy9wzX marked with a red arrow the ones that glitch. I thought it could have something to do with the layer but the prefab spawns always on the same layer since it is only 1 prefab with randomized sprite.
Am i missing something in the inspector or is it something i can fix with code?
Answer by Molthor · Apr 22, 2020 at 03:56 PM
Ok so i found out what i need, so when ever a new cloned prefab spawn i need to increment the layer position. so it wont appear in the back of the preview cloned prefab sprite. Gonna give it a try and will post code after if it either way works or not.
new update: didn't found a way,. here is the boxscript:
public class BoxScript : $$anonymous$$onoBehaviour { // a forma como spawna a andar para os lados private float $$anonymous$$_x = -2.2f, max_x = 2.2f;
private bool can$$anonymous$$ove;
private float move_speed = 3f; // velocidade com que move
private Rigidbody2D myBody;
private bool gameOver;
private bool ignoreCollision;
private bool ignoreTriggering;
// teste random sprite
private int rand;
public Sprite[] Sprite_Pic;
//
public int contadorDeLayer = 2;
private void Awake()
{
myBody = GetComponent<Rigidbody2D>();
myBody.gravityScale = 0f;
}
private void Start()
{
rand = Random.Range(0, Sprite_Pic.Length);
GetComponent<SpriteRenderer>().sprite = Sprite_Pic[rand]; // spawn with a random sprite of pre selected sprites
// renderer.sortingOrder = (contadorDeLayer + 1); here is where im trying to fix the order as it spawns.
can$$anonymous$$ove = true;
//radomizar movimento direito ou esquerdo
if(Random.Range(0, 2) > 0)
{
move_speed *= -1f; //randomisa o movimento
}
GameplayController.instance.currentBox = this;
contadorDeLayer++;
}
private void Update()
{
$$anonymous$$oveBox();
}
void $$anonymous$$oveBox()
{
if (can$$anonymous$$ove)
{
Vector3 temp = transform.position;
temp.x += move_speed * Time.deltaTime;
if(temp.x > max_x)
{
move_speed += -1f; // muda direçao
}
else if (temp.x < $$anonymous$$_x)
{
move_speed *= -1f;
}
transform.position = temp;
}
}
public void DropBox(){
can$$anonymous$$ove = false;
myBody.gravityScale = Random.Range(2, 4);
}
void Landed()
{
if (gameOver)
{
return;
}
ignoreCollision = true;
ignoreTriggering = true;
GameplayController.instance.SpawnNewBox();
GameplayController.instance.$$anonymous$$oveCamera();
}
void RestartGame()
{
GameplayController.instance.RestartGame();
}
void OnCollisionEnter2D(Collision2D target)
{
if (ignoreCollision)
{
return;
}
if (target.gameObject.tag == "Platform")
{
Invoke("Landed", 2f);
ignoreCollision = true;
}
if (target.gameObject.tag == "Box")
{
Invoke("Landed", 2f);
ignoreCollision = true;
}
}
void OnTriggerEnter2D(Collider2D target)
{
if (ignoreTriggering)
{
return;
}
if(target.tag == "GameOver")
{
CancelInvoke("Landed");
gameOver = true;
ignoreTriggering = true;
Invoke("RestartGame", 2f);
}
}
}