Can't change sprite of the Child from Parent

Hi, so i am trying to change the sprite of the child object to do some sort of “damage” visualization but when i try to access the spriterenderer from script it does nothing.
I made an array of sprites and thought it would be pretty easy to change the sprite when i’d want it to change but it doesn’t work, this is my code

(PS: i already tried the getComponentInChildren but it didn’t work so i tried to go directly by putting the gameobject i wanted the sprite renderer to change, to debug)

  SpriteRenderer SpriteChildRenderer;
    float maxHealth;
    float HealthState;
   [SerializeField] GameObject children;
    [SerializeField] Sprite[] canSprites;
   


    private void Start()
    {   

        SpriteChildRenderer = children.GetComponent<SpriteRenderer>();
        Integrity = 1;
        health= GetComponent<Health>();
     
        maxHealth = health.GetHealth();
        HealthState = maxHealth;

    }

    private void Update()
    {
        
        CheckState();
    }

it is nothing special it’s quite simple, all it does are some check but idk
this is where i check for the state i want the sprite to change

  private void CheckState()
    {

        HealthState= health.GetHealth();

        if (0.7f >= (HealthState / maxHealth))
        {
            Integrity = 0;
            SpriteChildRenderer.sprite = canSprites[Integrity];
        }

        if (0.5f >= (HealthState / maxHealth))
        {
            Integrity = 1;
            SpriteChildRenderer.sprite = canSprites[Integrity];
        }

        if (0.2f >= (HealthState / maxHealth))
        {
            Integrity = 2;
            SpriteChildRenderer.sprite = canSprites[Integrity];
        }


i’m trying to simply change the sprite from this
image

to this
image

image

but it does nothing, even if i try to change the sprite on START ,

this should be pretty easy but i don’t know what is going wrong .-.
image

this is the child

for debugging purposes i tried to do also simply

 private void Start()
    {   

        SpriteChildRenderer = children.GetComponent<SpriteRenderer>();
        SpriteChildRenderer.sprite = canSprites[2];

    }

but it still doesn’t change anything, the sprite remain the same.

i’m still trying and i discovered that if i put a SpriteRenderer and i try to use these logic onparent it does change the sprite, but why? i want the sprite render of the child to change, why this works only with parent

Hi Shinos,

Thanks a lot for sharing so much information on your project and what you tried. That’s really helpful. It’s also great to see that you tested code in your Start method and figured out that it did not work as expected.

What you shared makes sense, and I too feel that this should work. There is just a little problem somewhere which needs to get solved. Try to narrow it down further.

Check your console. If there isn’t any NullReferencException regarding SpriteChildRenderer.sprite, log the name of the game object to which the returned SpriteRenderer is attached into your console. Maybe you just got the “wrong” object here.

Also check the children variable. Maybe your object references a prefab. That would explain why you do not see any changes in your game.

1 Like

Hi! i know it’s been two weeks but i have been super ultra busy and i thank you with all my gratitude for your response! i will try this as soon as i can ( i know i said this also in the other answer you gave me haahah)

Take your time! :slight_smile:

1 Like

How are you getting on with this, @Shinos?

1 Like

Hi Nina i tinkered and checked it for a while but unfortunately the behavior is still the same, when i put the sprites in my array to change it doesn’t work, it only changes the sprite renderer of the parent if i try to but not the spriterenderer of the body which i want
i really can’t understand why i get the sprite changed from the parent but not from the Children when i also tried to reference the sprite renderer i wanted directly from the editor
i’ll get you see what i mean

    [SerializeField] Sprite[] canSprites;
    [SerializeField] SpriteRenderer SpriteChildRenderer;
  private void Update()
    {
        
       CheckState();
    }
 private void CheckState()
    {

        HealthState = health.GetHealth();

        if (0.7f >= (HealthState / maxHealth))
        {
            Integrity = 0;
          
             SpriteChildRenderer.sprite = canSprites[Integrity];
        }

        if (0.5f >= (HealthState / maxHealth))
        {
            Integrity = 1;
          
            SpriteChildRenderer.sprite = canSprites[Integrity];
        }

        if (0.2f >= (HealthState / maxHealth))
        {
            Integrity = 2;
           
           SpriteChildRenderer.sprite = canSprites[Integrity];
        }
    }

this is the code, nothing really special happening here
this is the parent prefab
image
the child
image

if i do this it doesn’t change the sprite, but if i for example put as a reference in the editor the sprite renderer of the parent, the sprites begin to change

if i add for example a sprite renderer on the parent and i do reference it , it does work
image
but i don’t want it to work on the parent because it would mess the sprite offset, and also it seems pretty weird to me for all of this to not work

Also the Debug Console is crystal clear while doing all this no errors

image
this is the child

Is it? I’m wondering if it’s just a random prefab.

When you dragged can_body into that field, did you drag it from the hierarchy or the project folder? We’re you in the prefab editor at the time?

If you pause the game while playing, does the children field show a blue cube?

1 Like

image
this is what i have when i pause the game( it’s a clone because it is a spawned object)
(i think that when i made that screenshoot i dragged it only in the editor just to show the hierarchy)

What about in the inspecter?

1 Like

ah sorry i saw now that you edited the message , i didn’t understand you wanted to see that so i took a screenshoot of everything ahahahah i’ll take a screenshoot of that section wait just a sec
image
as i said if i drag instead as a reference for Sprite Child Renderer a SpriteRenderer component placed on the parent, it works
i don’t know what else to do ahahaha if you have time try to see if using my script and by placing a parent and a body with a sprite renderer and 3 sprites in the array if it changes

That looks good and the screenshots answered my next question too.

Also, I noticed explosion_can has an animator. Try turning that off (uncheckmark it in the prefab) and see if that helps.

1 Like

Oh c*rap it’s working!!! :sob: :sob: :scream_cat:

but why? can’t i change sprites and use the animator(?) i had it there because i wanted to trigger an explosion animation, like the death one i had on other defenders

I just realized that i had until now a can_destroyed animation with a single sprite in it…
of course it would trigger it when starting and staying in that


image

i am really sorry to have taken your time for something as silly as this

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms