2

So i'm having this strange behavior when I set the color for a gameobject.

using UnityEngine;

public class TestObject: MonoBehaviour
{
private Material screenMaterial;

private void Awake()
{
    screenMaterial= transform.GetChild(0).GetComponent<SpriteRenderer>().material;
    screenMaterial.SetColor("_Color", Color.yellow);
}
}

enter image description here

The problem is the color is set to yellow correctly like 90% of the time and sometimes its just white. I mean the only thing that comes to my mind on why this happen is that there isn't enough time for the script to get the material component and then sets its color?!

This is a brand new 2D project and there no other scripts.

Any idea why this happen?

Here's the project file. Download. Made in unity 2019.2.18f1

7
  • Please include steps to reproduce the problem including what objects to create in the scene
    – Ruzihm
    May 20, 2020 at 21:30
  • Maybe white is default color and it's a timing issue when setting yellow color.
    – estinamir
    May 20, 2020 at 21:32
  • The steps are just one actually, I just press start and like 9 times outta a 10 the color is set to yellow or whatever color is want but just one time is just and yeah its default color. @Ruzihm
    – user11746000
    May 20, 2020 at 21:35
  • Yes the default color is white, and maybe its a timing issue but what is it? This is a new 2D project and this is the first script so whats going on? @bestinamir
    – user11746000
    May 20, 2020 at 21:37

4 Answers 4

2

It could be that the child is not always loaded in yet when Awake is called on this GameObject. I would try changing out Awake() for Start() and see if the problem persists.

1
  • I just tried that and its the same, I moved whats inside Awake() to Start() but nothing has changed.
    – user11746000
    May 20, 2020 at 21:39
2

You are currently trying to set the color of the material. Instead of doing that, just change the Color field seen in the SpriteRenderer Component below.

enter image description here

You can use the following code to adjust the color of a sprite:

public class TestObject: MonoBehaviour
{
    private Material screenMaterial;

    private void Awake()
    {
        transform.GetChild(0).GetComponent<SpriteRenderer>().color = Color.yellow;
    }
}

Hope this helps!

4
  • Its a good idea but this way the color is always white.
    – user11746000
    May 20, 2020 at 22:07
  • 1
    What do you mean? It should turn yellow. It stats off white but as soon as the game starts, it should turn to yellow. Let me take another look at your question now that you added the picture.
    – Jee
    May 20, 2020 at 23:21
  • i'm telling you it does turn to yellow/green/red...etc and this happens everytime i press the play button, its just 1 time outta of 10 it DOESN'T, why? thats what i'm trying to figure out.
    – user11746000
    May 20, 2020 at 23:24
  • I have uploaded the unity package file
    – user11746000
    May 20, 2020 at 23:32
1

So I managed to solve the issue and its actually very simple, Since the TestObject is made out of 2 objects the first one is the parent and the second one is the child..

All I had to do is to change the sorting layers, so by setting the the sorting layer in the child object to 1 and the parent's to 0 FIXED THE ISSUE.

I hope this help some of you guys.

1
  • You can accept your own answer so that anyone else with this problem can see what the solution was if they find this page in the future. May 22, 2020 at 17:36
0

try

transform.GetChild(0).GetComponent<SpriteRenderer>().material.color = Color.yellow;

The way you are setting color is like for a custom shader, setting the "_Color" property of the shader

edit try this:

for (var child : Transform in transform) {
    child.GetComponent<SpriteRenderer>().material.color = Color.yellow;
}
9
  • Thanks for your help, its still the same, why?
    – user11746000
    May 20, 2020 at 21:46
  • do you have two materials on the object?
    – vasmos
    May 20, 2020 at 21:46
  • Yes I do, the first one is (Body) and the second one is (Screen) which i'm getting its component in the Awake() method and its a child of the test object.
    – user11746000
    May 20, 2020 at 21:52
  • okay I edited my answer and gave something else to try
    – vasmos
    May 20, 2020 at 21:57
  • Again thank you but its still the same.
    – user11746000
    May 20, 2020 at 22:04