Comparing Colors in Mesh

I am trying to obtain the Color list along with the array point that it is associated with while colliding, I have a whole mesh set up with colors on each vertex, While colliding I am attempting to collect the color of the colliding vertex and compare the two colors. Though I am unable to figure out how to associate them with each other. If someone could help me with this I would be very grateful as this is an important part of the project.

Here is a snippet:

if(gameObject.CompareTag("Organism") && other.gameObject.CompareTag("Organism") && isAlive == true) {

            Color thisColor = gameObject.GetComponent<OrganismCreation>().colors[Random.Range(0, gameObject.GetComponent<OrganismCreation>().colors.Count)];
            Color otherColor = other.gameObject.GetComponent<OrganismCreation>().colors[Random.Range(0, other.gameObject.GetComponent<OrganismCreation>().colors.Count)];

            //if this color is in the Red zone and the other color is in the Green zone
            if((thisColor.r > otherColor.g) && (organismMutationsScript.isHerbivore == true && other.gameObject.GetComponent<Mutations>().isPlant == true) && other.gameObject.GetComponent<OrganismScript>().biomassCount >= 0.001f * gameObject.GetComponent<Mutations>().herbivoreMultiplier * other.gameObject.GetComponent<Mutations>().plantMultiplier && biomassCount <= organismMutationsScript.maxBiomass) {

                biomassCount += 0.001f * gameObject.GetComponent<Mutations>().herbivoreMultiplier * other.gameObject.GetComponent<Mutations>().plantMultiplier;
                other.gameObject.GetComponent<OrganismScript>().biomassCount -= 0.001f * gameObject.GetComponent<Mutations>().herbivoreMultiplier * other.gameObject.GetComponent<Mutations>().plantMultiplier;
            }
            //if this color is in the Yellow zone and the other color is in the Red zone
            if(((thisColor.r + thisColor.g) / 2) > otherColor.r && (organismMutationsScript.isCarnivore == true && other.gameObject.GetComponent<Mutations>().isHerbivore == true) && other.gameObject.GetComponent<OrganismScript>().biomassCount >= 0.001f * gameObject.GetComponent<Mutations>().carnivoreMultiplier && biomassCount <= organismMutationsScript.maxBiomass) {

                biomassCount += 0.001f * gameObject.GetComponent<Mutations>().carnivoreMultiplier;
                other.gameObject.GetComponent<OrganismScript>().biomassCount -= 0.001f * gameObject.GetComponent<Mutations>().carnivoreMultiplier;
            }
        }

Wow, me neither. The above code should probably be about 40-50 lines long so you can have a chance of reasoning about what is even going on!

If you have more than one or two dots (.) in a single statement, you’re just being mean to yourself.

How to break down hairy lines of code:

http://plbm.com/?p=248

Break it up, practice social distancing in your code, one thing per line please.

Keep in mind that you can’t compare floating points numbers for equality.

https://starmanta.gitbooks.io/unitytipsredux/content/floating-point.html

I’m more at the start of my code here, after this part pans out ill see about refactoring the code. I just need it to work in the first place.

are all the vertices same color? (as you are picking random color from the array now)

to get closest point on collision, then would have to iterate vertices to find closest to that point:

Figured it out: here is my code now:

  public void OnCollisionEnter(Collision other) {

        if(gameObject.CompareTag("Organism") && other.gameObject.CompareTag("Organism") && isAlive == true) {

            ContactPoint[] contacts = new ContactPoint[1];
            var contactPoint = contacts[0].point;
            OrganismCreation otherCreationScript = other.gameObject.GetComponent<OrganismCreation>();
            Mutations otherMutationScript = other.gameObject.GetComponent<Mutations>();
            OrganismScript otherOrganismScript = other.gameObject.GetComponent<OrganismScript>();

            if((organismMutationsScript.isHerbivore == true && otherMutationScript.isPlant == true)) {

                if(biomassCount <= organismMutationsScript.maxBiomass) {

                    if(otherOrganismScript.biomassCount >= 0.001f * (organismMutationsScript.herbivoreMultiplier * organismMutationsScript.attackMultiplier * otherMutationScript.plantMultiplier) / (otherMutationScript.defenseMultiplier + 1)) {

                        for(int b = 0; b < otherCreationScript.verties.Count; b++) {

                            if(otherCreationScript.verties[b] == contactPoint) {

                                otherColor = otherCreationScript.colors[b];
                            }
                        }

                        if((organismCreationScript.colors[Random.Range(0, organismCreationScript.colors.Count - 1)].r > otherColor.g)) {

                            biomassCount += 0.001f * otherMutationScript.herbivoreMultiplier * otherMutationScript.plantMultiplier;
                            other.gameObject.GetComponent<OrganismScript>().biomassCount -= 0.001f * organismMutationsScript.herbivoreMultiplier * otherMutationScript.plantMultiplier;
                        }
                    }
                }
            }
            if(organismMutationsScript.isCarnivore == true && otherMutationScript.isHerbivore == true) {

                if(biomassCount <= organismMutationsScript.maxBiomass) {

                    if(otherOrganismScript.biomassCount >= 0.001f * organismMutationsScript.carnivoreMultiplier) {

                        for(int b = 0; b < otherCreationScript.verties.Count; b++) {

                            if(otherCreationScript.verties[b] == contactPoint) {

                                otherColor = otherCreationScript.colors[b];
                            }
                        }

                        if(((organismCreationScript.colors[Random.Range(0, organismCreationScript.colors.Count - 1)].r + organismCreationScript.colors[Random.Range(0, organismCreationScript.colors.Count - 1)].g) / 2 > otherColor.r)) {

                            biomassCount += 0.001f * otherMutationScript.carnivoreMultiplier;
                            otherOrganismScript.biomassCount -= 0.001f * organismMutationsScript.carnivoreMultiplier;
                        }
                    }
                }
            }
        }
    }
}

Though I don’t know how to get “This” color