Answer the question
In order to leave comments, you need to log in
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);
}
}
}
}
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>();
}
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);
}
}
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) prefabColor[ci].r
, but this also did not help
Answer the question
In order to leave comments, you need to log in
In general, I found a solution, namely: I renamed all the necessary objects in the unit to numbers
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 questionAsk a Question
731 491 924 answers to any question