How to store sprite renderer color in script?

Hey, i have parent object and children object and i need to store color of children object sprite renderer at start and then access it from parent object via script. Could someone help me please?

It’s like ANY Component in Unity: make a public field for the SpriteRenderer in your script, put the script on a GameObject, drag the SpriteRenderer instance into it. Then in code access the Component to get whatever you want, in this case the .color field.

If ANYTHING I wrote above is even REMOTELY confusing to you, instantly set your project aside and work through some basic Unity3D tutorials because this is just critical to working in Unity. This type of question and function is tantamount to learning how to eat: we use it every single step of every possible thing we do in the Unity context.

yeah i know how it work etc. but i just coulden´t make this thing work

Well step through the parts listed above. We know it works.

If you have a dog, call him over and explain each step to him the way you see me doing in my avatar post.

Remember that with software, anything short of 100% complete often does not work, so work carefully.

Don’t forget to give your dog a treat.

Hard to know exactly how to help you based on limited info you give us on your data set up, how you create the children etc etc but here are a few ideas to get you started:

You can store an array of colors like this:

public Color32[] MyChildrensColors= new Color32[50];  // Subsititue 50 for the max number of children;

You can access the color like this:

var mycolor = MyChildrensColors[childnumber];

And you can set the childs sprite color like this:

var myrend = spawnedChild.GetComponent<SpriteRenderer>();
myrend.color=mycolor;

Not working code you can cut and paste into your project, but a hint at one way you could solve your problem.
Hope this helps, good luck!

1 Like