W
W
WasTabon2021-04-02 15:43:36
Unity
WasTabon, 2021-04-02 15:43:36

How to compare objects by color?

I tried to do it like here - https://coderoad.ru/39349906/Unity-how-to-check-with... and it didn't help
Here is the code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class CourierWorkDragnDrop : MonoBehaviour, IDragHandler
{

    public AllVariables allVariables;
    private Rigidbody2D rigidbody2D;
    private Vector2 vector2;
    public Works works;
    public SpriteRenderer spriteRenderer;

private void OnCollisionEnter2D(Collision2D coll)
    {
        if (coll.gameObject.CompareTag("Shop"))
        {
            Color myColor = spriteRenderer.color;
            Color otherColor = works.spriteRendererSHOP.color;
            if (myColor == otherColor)
            {
                allVariables.money += 5;
                works.howMuchEarn += 5;
                works.textMoneyCourierEarn.text = "Заработано - " + works.howMuchEarn;
                Destroy(gameObject);
            }
        }
    }
}

Here is the code for getting the SpriteRenderer
using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;

public class Works : MonoBehaviour
{
    public SpriteRenderer spriteRendererSHOP;
    private void Awake()
    {
        spriteRendererSHOP = shop.GetComponent<SpriteRenderer>();
    }

And here is the script itself, which changes the color of the object
public IEnumerator CourierWorkShopColor()
    {
        while(true)
        {
            //Debug.Log("Started!");
            System.Random r = new System.Random();
            int ci = r.Next(0, coloredPrefabs.Length);
            spriteRendererSHOP.color = new Color32(prefabColor[ci].r, prefabColor[ci].g, prefabColor[ci].b, 255);
            yield return new WaitForSeconds(3.5f);
        }
    }

Transparency was not specified prefabColor[ci]as 255 because the object simply disappears (for some reason, the transparency is set to 0, although the object itself was at 255)
I tried to do the same comparison only by prefabColor[ci].r, but this also did not help

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WasTabon, 2021-04-02
@WasTabon

In general, I found a solution, namely: I renamed all the necessary objects in the unit to numbers 6067281fcc6f6222192563.png
And I did this

private void OnCollisionEnter2D(Collision2D coll)
    {
        if (coll.gameObject.CompareTag("Shop") && gameObject.name == works.ci.ToString())
        {
            allVariables.money += 5;
            works.howMuchEarn += 5;
            works.textMoneyCourierEarn.text = "Заработано - " + works.howMuchEarn;
            Destroy(gameObject);
        }
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question