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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Tintcolor won't change

Discussion in 'Scripting' started by ellenblomw, Aug 4, 2018.

  1. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
    Hello,

    I have a problem with a clones tint color. This is the code I'm working with:

    Code (CSharp):
    1.  
    2.     void Start() {
    3.             if (GameObject.Find("1Pappa(Clone)") != null)
    4.         {
    5.             GameObject.Find("1Pappa(Clone)").GetComponent<Renderer>().material.SetColor("_TintColor", new Color(181, 191, 19, 162));
    6.             Debug.Log("clone found");
    7.         }
    8.     }
    9.  
    I want to be able to set the skin tone of the instantiated object. So if the object instantiated is 1Pappa(Clone), then I want the skin tone to change. So I have a material with Shader=Sprites default and a tint, and when I change the tint color in the editor the skin tone on the sprite changes. But when I try to do it via the code above, nothing happens on the sprite. Does anyone know why? I don't get any error in the console.

    Also I don't get any debug log message in the console window.
     
    Last edited: Aug 4, 2018
  2. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
    I tried if (GameObject.Find("1Pappa(Clone)") == true) without any success
     
  3. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    You can cache the result of the first call to 'GameObject.Find' in order to save the overhead for the second call, however, I'd recommend to avoid the Find method completely (it's error prone and relatively slow).

    Other than that, if you're dealing with Sprites, you're also dealing with SpriteRenderers. SpriteRenderers allow to change the tint color independently of the material. That is, try to use GetComponent<SpriteRenderer> and then set the color directly.

    Code (csharp):
    1.  var spriteRenderer = yourObject.GetComponent<SpriteRenderer>();
    2. if (spriteRenderer != null)
    3. {
    4.     spriteRenderer.color = new Color(...);
    5. }
     
  4. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
    Well I have a sort of special situation so I'm not quite sure how to avoid gameobject.find. I want to set the tint of the character body to that of the main character choosen. So the body is the same for character 1-4 (so I only have to animate 1 body), but the tint changes according to what character has been saved to playerprefs.

    Code (CSharp):
    1.         //Instantiate(Parent1[PlayerPrefs.GetInt("Parent1Number") - 1]);
    2.         GameObject parent1Prefab = Parent1[PlayerPrefs.GetInt("Parent1Number") - 1];
    3.         GameObject parent1Handle = Instantiate(parent1Prefab, new Vector3(20, 1, -1), Quaternion.Euler(Vector3.zero), GameObject.Find("page1").transform);
    4.  
    The GameObject.find is used to see if a certain sprite has instantiated. What would be a better way to do this?
     
  5. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    I just noticed that I missed this one:

    which probably refers to the line that calls Debug.Log in your snippet.

    I could imagine that's due to the missing space between the name and 'Clone()'. Perhaps there's another typo though, which also supports the argument that you should try to avoid using Find.

    At some point you have to instantiate said object. You can then cache it in a variable and don't need to use GameObject.Find at all.

    Anyway, first make sure that you find the object. Then apply the solution I provided above.
    Afterwards you can try to replace the call to GameObject.Find, for now you can concentrate on finding the object this way and get the actual problem fixed.
     
  6. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
  7. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Are you sure it's already in the scene when the above Start method runs? Perhaps the game object is inactive? Does your script even run or is its gameobject inactive or the script disabled?
     
  8. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
    HAHA I had deactivate "i" in the console. So it's been there all the time. sweet n00b mistake :p.

    But it's still not changing color:
    Code (CSharp):
    1.  GameObject.Find("1Pappa(Clone)").GetComponent<Renderer>().material.SetColor("_TintColor", new Color(181, 191, 19, 162));
    2.            
    Do you know how I can fix that?

    "At some point you have to instantiate said object. You can then cache it in a variable and don't need to use GameObject.Find at all."

    I only instantiate one of them depending on chosen character. Do I just make var for each instantiated prefab even though it might not show up then? And how do I handle them being clones :S?
     
    Last edited: Aug 4, 2018
  9. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Check my first post.
    You're dealing with a SpriteRenderer. SpriteRenderers expose a color property that allow to mess with the tint color and do not require access to the material.

    As for the instantiation, at some point you're going to instantiate the game object and that's where you can keep track of that object in a variable. A singleton pattern might be useful, but if that's too much at the moment, you should get away with the current approach.
     
  10. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
    Hmm ok I think I understand! Can I set alpha of colour on sprite renderer too? Because when at first I used the sprite renderer it made my sprite become just that one color, not as an overlay as with the shader.

    I have all my sprites added to this list:
    • public List<GameObject> Parent1;
    In the editor.

    Can I access them this way maybe and skip .Find?

    Code (CSharp):
    1.     public List<GameObject> Parent1;
    2.  
    3.     // Use this for initialization
    4.     void Start()
    5.     {
    6.         //Instantiate(Parent1[PlayerPrefs.GetInt("Parent1Number") - 1]);
    7.         GameObject parent1Prefab = Parent1[PlayerPrefs.GetInt("Parent1Number") - 1];
    8.         GameObject parent1Handle = Instantiate(parent1Prefab, new Vector3(20, 1, -1), Quaternion.Euler(Vector3.zero), GameObject.Find("page1").transform);
    9.         if (PlayerPrefs.GetInt("Parent1Number") == 1) {
    10.             Debug.Log(PlayerPrefs.GetInt("Parent1Number"));
    11.             Parent1(1).GetComponent<Renderer>().material.SetColor("_TintColor", new Color(181, 191, 19, 162));
    12.         }
    Can I somehow access objects on that Parent1 list? Like calling them in someway? Parent1(1).GetComponent<Renderer>().material.SetColor("_TintColor", new Color(181, 191, 19, 162))
     
  11. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
    PS thank you for all your help so far!
     
  12. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
    Got it working :)!