Tim Cooley
Tim Cooley

Reputation: 759

How do I color in Unity using if statements?

I am having a problem with coloring. I think I have some order of operations issue going on, but I really am not sure what is going on.

Here is an overview of the problem. I have a CrowdManager script that changes the crowd.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class CrowdManager : MonoBehaviour
{
    public enum crowdOptions
    {None,TeamA,TeamB};

    public crowdOptions CrowdOptions;

    public static CrowdManager instance = null;

    void Awake ()
    {
        if (instance == null) {
            instance = this;
        } else if (instance != this) {
            Destroy (this.gameObject);
        }
    }

    // Use this for initialization
    void Start ()
    {
        ChangeCrowd ();
    }
}
public void ChangeCrowd ()
{
    if (PlayerPrefs.HasKey ("crowd")) {
        instance.crowdCount = PlayerPrefs.GetInt ("crowd");
        print (instance.crowdCount);
    } else {
        PlayerPrefs.SetInt ("crowd", instance.crowdCount);
    }
}

    if (instance.crowdCount <= 0) {
        instance.crowdCount += Random.Range (1, 3);
        PlayerPrefs.SetInt ("crowd", instance.crowdCount);

        if (Random.value < .33) {
            instance.CrowdOptions = crowdOptions.None;

        } else if (Random.value > .66) {
            instance.CrowdOptions = crowdOptions.TeamA;

        } else {
            instance.CrowdOptions = crowdOptions.TeamB;

        }
        // Change the crowd in the game!
    } 

    print (instance.CrowdOptions);
}

This randomly cycles between the three states, teamA, TeamB and None. This works fine. I even print which Team that the game has been changed to. The reason for this is that based on the team it is suppose to color my characters. I have attached a slimmed down version of the color code.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using Spine;

public class WinManager : MonoBehaviour
{
    public static WinManager instance = null;

    public Color teamNoneColor;
    public Color teamAColor;
    public Color teamBColor;

    public Color crowdTeamColor;

    public Color[] teamAColors = new Color[4];
    public Color[] teamBColors = new Color[4];

    void Awake ()
    {
        if (instance == null) {
            instance = this;
        } else if (instance != this) {
            Destroy (this.gameObject);
        }
    }

    // Use this for initialization
    void Start ()
    {
        TeamColorsSelected ();
    }

    // Update is called once per frame last
    void LateUpdate ()
    {
        if (CrowdManager.instance.CrowdOptions == CrowdManager.crowdOptions.None) {
            //script
        }
        if (CrowdManager.instance.CrowdOptions == CrowdManager.crowdOptions.TeamA) {
            //script
        }
        if (CrowdManager.instance.CrowdOptions == CrowdManager.crowdOptions.TeamB) {
            //script
        }
    }

    public void TeamColorsSelected ()
    {
        //Team A Colors
        teamAColors [0] = new Color (.10f, .35f, .75f, 1);
        teamAColors [1] = new Color (.37f, .19f, .58f, 1);
        teamAColors [2] = new Color (.32f, .01f, .02f, 1);
        teamAColors [3] = new Color (1f, .58f, .14f, 1);

        //Team B Colors
        teamBColors [0] = new Color (.39f, .60f, .83f, 1);
        teamBColors [1] = new Color (0f, .30f, .15f, 1);
        teamBColors [2] = new Color (1f, .89f, .17f, 1);
        teamBColors [3] = new Color (0f, .14f, .29f, 1);

        teamAColor = teamAColors [Random.Range (0, teamAColors.Length)];
        teamBColor = teamBColors [Random.Range (0, teamBColors.Length)];
        teamNoneColor = new Color (0, 1, 0, 1); //green

        if (CrowdManager.instance.CrowdOptions == CrowdManager.crowdOptions.TeamB) {
            crowdTeamColor = teamBColor;
        }
        if (CrowdManager.instance.CrowdOptions == CrowdManager.crowdOptions.TeamA) {
            crowdTeamColor = teamAColor;
        }
        if (CrowdManager.instance.CrowdOptions == CrowdManager.crowdOptions.None) {
            crowdTeamColor = teamNoneColor;
        }

        //CrowdMember Coloring
        teamCrowdMember = GameObject.Find ("RightCrowdMember").GetComponent<Renderer> ();
        crowdTeamMemberColor = teamCrowdMember.gameObject.GetComponent<SkeletonAnimation> ();

        TeamColorUpdate ();
    }

    public void TeamColorUpdate ()
    {
        //CrowdManager Clothing Colors
        crowdTeamMemberColor.skeleton.FindSlot ("torso").SetColor (crowdTeamColor);
        crowdTeamMemberColor.skeleton.FindSlot ("sleeve_left").SetColor (crowdTeamColor);
        crowdTeamMemberColor.skeleton.FindSlot ("sleeve_right").SetColor (crowdTeamColor);
    }
}

The problem is in this area:

if (CrowdManager.instance.CrowdOptions == CrowdManager.crowdOptions.TeamB) {
    crowdTeamColor = teamBColor;
            }
if (CrowdManager.instance.CrowdOptions == CrowdManager.crowdOptions.TeamA) {
    crowdTeamColor = teamAColor;
            }
if (CrowdManager.instance.CrowdOptions == CrowdManager.crowdOptions.None) {
    crowdTeamColor = teamNoneColor;
            }

If I set the crowdTeamColor = teamNoneColor or any of the other direct colors. the crowdTeamColor works, but with the if statement the first time the game loads it sets it to whatever the first teams color was and then doesn't change after that. Even though the script is running in the Start() I have tried doing if else statements as well I thought maybe separating them individually would force it to work properly.

Any help would be greatly appreciated. Sorry for the code vomit, but I felt like I needed to show everything related to this issue.

Here is a link to the "Solution" (work in progress): https://stackoverflow.com/questions/35145664/how-do-i-color-in-unity-using-is-statements-solution

Upvotes: 0

Views: 389

Answers (2)

Tim Cooley
Tim Cooley

Reputation: 759

Here is the answer to the post. Thanks for the help! Sorry for making things confusing :-)

Note I have cleaned up some of the code to make it easier to read in chunks.

CrowdManager.cs

using System.Collections;
using UnityEngine.UI;

public class CrowdManager : MonoBehaviour
{
using System.Collections;
using UnityEngine.UI;

public class CrowdManager : MonoBehaviour
{
    public enum CrowdTypes
    {
        None,
        TeamA,
        TeamB}
    ;

    public CrowdTypes crowdOptions;

    public int crowdCount = 5;
    public float crowdRandomRole;

    // Use this for initialization
    void Start ()
    {
        ChangeCrowd ();
    }

    public void ChangeCrowd ()
    {
        if (crowdCount <= 0) {
            crowdCount += Random.Range (1, 3);
            PlayerPrefs.SetInt ("crowd", crowdCount);

            crowdRandomRole = Random.value;

            if (crowdRandomRole < .33) {
                crowdOptions = CrowdTypes.None;
            } else if (crowdRandomRole > .66) {
                crowdOptions = CrowdTypes.TeamA;
            } else {
                crowdOptions = CrowdTypes.TeamB;
            }
    }
}

WinManager.cs

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using Spine;


public class WinManager : MonoBehaviour
{
    public CrowdManager cm;

    public bool yayButton = false;
    public bool booButton = false;

    public Color teamNoneColor;
    public Color teamAColor;
    public Color teamBColor;

    public Color crowdTeamColor;

    // Use this for initialization
    void Start ()
    {
        SetTeamColors ();
        TeamColorsSelected ();
    }

    // Update is called once per frame
    void Update ()
    {
        rightCrowdMemberAnimate = GameObject.Find ("RightCrowdMember").GetComponent<SkeletonAnimation> ();
        leftCrowdMemberAnimate = GameObject.Find ("LeftCrowdMember").GetComponent<SkeletonAnimation> ();


        if (cm.crowdOptions == CrowdManager.CrowdTypes.None) {

            //script
        }
        if (cm.crowdOptions == CrowdManager.CrowdTypes.TeamA) {

            //script
        }
        if (cm.crowdOptions == CrowdManager.CrowdTypes.TeamB) {

            //script
        }
    }

    public void SetTeamColors ()
    {
        //Team A Colors
        teamAColors [0] = new Color (.10f, .35f, .75f, 1f);
        teamAColors [1] = new Color (.37f, .19f, .58f, 1f);
        teamAColors [2] = new Color (.32f, .01f, .02f, 1f);
        teamAColors [3] = new Color (1f, .58f, .14f, 1f);

        //Team B Colors
        teamBColors [0] = new Color (.39f, .60f, .83f, 1f);
        teamBColors [1] = new Color (0f, .30f, .15f, 1f);
        teamBColors [2] = new Color (1f, .89f, .17f, 1f);
        teamBColors [3] = new Color (0f, .14f, .29f, 1f);

        teamAColor = teamAColors [Random.Range (0, 3)];
        teamBColor = teamBColors [Random.Range (0, 3)];
        teamNoneColor = new Color (0f, 1f, 0f, 1f); //green

    }

    public void TeamColorsSelected ()
    {

        //ScoreBoard Team Colors
        Image scoreBoardRed = GameObject.Find ("ScoreBoardRed").GetComponent<Image> ();
        scoreBoardRed.color = teamAColor;
        Image scoreBoardBlue = GameObject.Find ("ScoreBoardBlue").GetComponent<Image> ();
        scoreBoardBlue.color = teamBColor;

        //  crowdTeamColor = teamAColor;

        if (cm.crowdOptions == CrowdManager.CrowdTypes.TeamB) {
            crowdTeamColor = teamBColor;
        }
        if (cm.crowdOptions == CrowdManager.CrowdTypes.TeamA) {
            crowdTeamColor = teamAColor;
        }
        if (cm.crowdOptions == CrowdManager.CrowdTypes.None) {
            crowdTeamColor = teamNoneColor;
        } 

        //CrowdMember Coloring
        rightCrowdMemberRender = GameObject.Find ("RightCrowdMember").GetComponent<Renderer> ();
        rightCrowdMemberColor = rightCrowdMemberRender.gameObject.GetComponent<SkeletonAnimation> ();

        leftCrowdMemberRender = GameObject.Find ("LeftCrowdMember").GetComponent<Renderer> ();
        leftCrowdMemberColor = leftCrowdMemberRender.gameObject.GetComponent<SkeletonAnimation> ();

        TeamColorUpdate ();
    }

    public void TeamColorUpdate ()
    {
        //rightTeamMember Color Jersey
        rightCrowdMemberColor.skeleton.FindSlot ("torso").SetColor (crowdTeamColor);
        rightCrowdMemberColor.skeleton.FindSlot ("sleeve_left").SetColor (crowdTeamColor);
        rightCrowdMemberColor.skeleton.FindSlot ("sleeve_right").SetColor (crowdTeamColor);

        //rightTeamMember Color Jersey
        leftCrowdMemberColor.skeleton.FindSlot ("torso").SetColor (crowdTeamColor);
        leftCrowdMemberColor.skeleton.FindSlot ("sleeve_left").SetColor (crowdTeamColor);
        leftCrowdMemberColor.skeleton.FindSlot ("sleeve_right").SetColor (crowdTeamColor);

    }
}

I figured it out. I needed to Update the

TeamColorsSelected ()

And it all worked! Thanks everyone for their help.

Upvotes: 1

Bohdan Yarema
Bohdan Yarema

Reputation: 121

If I get the problem right then try using switch case statement with default branch. Something like this might work.

switch (CrowdManager.instance.CrowdOptions) {
    case CrowdManager.crowdOptions.TeamB : 
        crowdTeamColor = teamBColor;
        break;
    case CrowdManager.crowdOptions.TeamA : 
        crowdTeamColor = teamAColor;
        break;
    case CrowdManager.crowdOptions.None :
    default : 
        crowdTeamColor = teamNoneColor;
        break;
}

Also CrowdManager.crowdOptions should be enum. If it is not a basic type then try using Equals method instead of == statement.

Upvotes: 0

Related Questions