Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question Sorting Order

Discussion in 'Scripting' started by artistshc, Apr 12, 2023.

  1. artistshc

    artistshc

    Joined:
    Mar 7, 2015
    Posts:
    79
    Hi there. I instantiated an object(in this case a fried egg) and I need to change the Sprite Renderer's order in layer and it isn't working. Please help.


    public Transform friedEgg;
    public Transform eggSpawn;

    IEnumerator OnTriggerEnter2D(Collider2D other) {
    if (isInCabinet == false) {
    if ((other.gameObject.name == "Pan") || (other.gameObject.name == "Pot")) {
    //then collided is true
    collided = true;
    //wait for 1 seconds and if still collided then...
    yield return new WaitForSeconds (2);
    if (collided) {
    //set the animation trigger bool atPan to true to start animation
    animator.SetTrigger ("atPan");
    //set animation speed at 1 to play
    animator.speed = 1;
    Instantiate (friedEgg, new Vector3 (eggSpawn.transform.position.x, eggSpawn.transform.position.y, 0), Quaternion.identity);

    ////////////////////////////////////////////////////////////////////////////////////////////
    ////RIGHT HERE- WHAT DO I DO//////////////////////////////////////
    friedEgg.GetComponents<SpriteRenderer>().
    ////////////////////////////////////////////////////////////////////////////////////////////

    //leave behind the egg shell
    Instantiate (leftoverShell, new Vector3 (shellSpawn.transform.position.x, shellSpawn.transform.position.y, 0), Quaternion.identity);
    //Destroy the egg
    Destroy(gameObject);
    }

    }

    } //end if (isInCabinet == false)

    Thank you!
     
  2. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    Code (CSharp):
    1. SpriteRenderer spriteRenderer = friedEgg.GetComponent<SpriteRenderer>();
    2. spriteRenderer.sortingOrder = 100; // or whatever
    If you have many sprite renderers on that object, then you can loop through them too.
    Code (CSharp):
    1. SpriteRenderer[] spriteRenderers = friedEgg.GetComponents<SpriteRenderer>();
    2. for(int i=0; i < spriteRenderers.length; i++)
    3. {
    4.    spriteRenderers[i].sortingOrder = 100; // or whatever
    5. }
    EDIT: I see other problems, though. Looks like "friedEgg" is probably a reference to your prefab. It definitely isn't a reference to the instance that you created. For that, you'll need to grab the output of Instantiate.
    Code (CSharp):
    1. GameObject eggObject = Instantiate (friedEgg, new Vector3 (eggSpawn.transform.position.x, eggSpawn.transform.position.y, 0), Quaternion.identity);
    2.  
    3. // Now reference the instance, not the prefab
    4. SpriteRenderer spriteRenderer = eggObject.GetComponent<SpriteRenderer>(); // eggObject is the instance
    5. spriteRenderer.sortingOrder = 100; // or whatever
     
    Last edited: Apr 12, 2023
    artistshc likes this.
  3. artistshc

    artistshc

    Joined:
    Mar 7, 2015
    Posts:
    79
    Thank you thank you thank you!!!!