Hi,
I have 3 types of Prefab objects that I spawn on a randomised location on the screen every X seconds.
I run a script that handles the spawning but, what actually happens is that one object is spawned on one layer and the other object is spawned on other layer, but not on top of the other, but below.
How I can make sure that every spawned object is shown on top of all other objects?
What am I missing here?
TNX
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PopupSpawner : MonoBehaviour {
public GameObject[] PopupsPrefabs;
Vector2 min;
Vector3 max;
public float spawnRate;
public float spawnStart;
Renderer MyRenderer;
public int MySortingLayer;
public int MySortingOrderInLayer;
// Use this for initialization
void Start () {
min = Camera.main.ViewportToWorldPoint (new Vector3 (0,0));
max = Camera.main.ViewportToWorldPoint (new Vector3 (1,1));
InvokeRepeating ("spawnPopups",spawnStart,spawnRate);
MyRenderer = GetComponent<Renderer> ();
}
// Update is called once per frame
void Update () {
}
void spawnPopups(){
//randomiziong a point between 0-1 on X Axis
// myRenderer = GetComponent<Renderer>().sortingOrder;
// Debug.Log (myRenderer);
int randomPopup = Random.Range (0,PopupsPrefabs.Length);
float popupExtentsX = PopupsPrefabs [randomPopup].GetComponent<Renderer> ().bounds.extents.x;
float popupExtentsY = PopupsPrefabs [randomPopup].GetComponent<Renderer> ().bounds.extents.y;
float randomX = Random.Range(min.x+popupExtentsX,max.x-popupExtentsX);
float randomY = Random.Range (min.y+popupExtentsY,max.y-popupExtentsY);
Vector2 randomPosition = new Vector2 (randomX,randomY);
Instantiate (PopupsPrefabs[randomPopup],randomPosition,Quaternion.identity );
MyRenderer.sortingOrder++;
// myRenderer=myRenderer+1;
// Debug.Log (myRenderer);
}
}