Can't assign Sprite Rendrer from Script

Hello everyone,
I am trying to assign a Sprite Renderer from script in OnEnable. First time it’s assigning the Sprite Renderer right, but from second time it’s not assigning the sprite renderer.

Every time this object enable, I am doing these things in the script:

  1. Destroying first child object.
  2. Instantiating an object as a child. Child object already have a Sprite Renderer attached.
  3. Getting Child object Sprite Renderer.

This is the script:

public SpriteRenderer childSpriteRend;
void OnEnable()
    {
        if(shipNumber == 1)
        {
            if(PlayerPrefs.HasKey("SlotShip1Slot"))
            {
                if(transform.childCount > 0)
                {
                    Destroy(transform.GetChild(0).gameObject);
                }

                foreach(GameObject obj in shipCards)
                {
                    if(obj.name == PlayerPrefs.GetString("SlotShip1Slot"))
                    {
                        Instantiate(obj, transform);
                        break;
                    }
                }
            }
            else if(transform.childCount > 0)
            {
                Destroy(transform.GetChild(0).gameObject);
            }
        }

        if(transform.childCount > 0)
        {
            childSpriteRend = transform.GetChild(0).GetComponent<SpriteRenderer>();
            Debug.Log(transform.GetChild(0).GetComponent<SpriteRenderer>());
        }
    }

In Debug.Log it’s showing sprite renderer object name and I can clearly see child object is there with Sprite Renderer.

Let me know if there any doubt.

OnEnable() is not a great place to do things with ANYTHING except your own self. The reason is because it happens so early and intrinsically to the script lifecycle. Instantiating and destroying other things is probably not going to work.

You could just set a flag saying something has to happen, then handle it in Update().

Here’s why:

1 Like

Thanks for the reply. Actually Destroying and Instantiating is happening perfectly fine. My game is paused when I run this script. So maybe this is something related to the time. I solved my problem with this:

void OnEnable()
    {
        if(PlayerPrefs.HasKey("SlotShip"+shipNumber+"Slot"))
        {
            if(transform.childCount > 0)
            {
                    Destroy(transform.GetChild(0).gameObject);
            }

            foreach(GameObject obj in shipCards)
            {
                if(obj.name == PlayerPrefs.GetString("SlotShip"+shipNumber+"Slot"))
                {
                    Instantiate(obj, transform);
                    break;
                }
            }
        }
        else if(transform.childCount > 0)
        {
            Destroy(transform.GetChild(0).gameObject);
        }
        StartCoroutine(SetChildSprite());
    }

    IEnumerator SetChildSprite()
    {
        yield return new WaitForSecondsRealtime(0.01f);
        if(transform.childCount > 0)
        {
            childSpriteRend = transform.GetChild(0).GetComponent<SpriteRenderer>();
            Debug.Log(transform.GetChild(0).GetComponent<SpriteRenderer>());
        }
    }