- Home /
Question by
YusufAfacan · Feb 26 at 11:58 AM ·
scripting problemscripting beginnermaterial colormaterial renderer
How to pass color of two colliding gameobjects to each other?
Hi everybody,
İ want to make two game objects give their color to each other when they collide for a puzzle game. And also change moving direction but i succeeded it. I have the following code in my game objects named marbles besides components attached: rigidbody, sphere collider, mesh renderer and material. Marbles also have tags based on their colors. I.e. an indigo marble tag with Indigo. Marble colors are after rainbow colors so i have seven types of marbles, colors and corresponding tags. Thanks in advance.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Marble : MonoBehaviour
{
public float speed;
public Color color;
// Start is called before the first frame update
void Start()
{
color = gameObject.GetComponent<MeshRenderer>().material.color;
}
// Update is called once per frame
void Update()
{
Move();
}
private void Move()
{
transform.position += transform.forward * Time.deltaTime * speed;
}
private void OnCollisionEnter(Collision other)
{
if (other.gameObject.CompareTag("Blue"))
{
speed = -speed;
color = new Color(0, 0, 255);
}
if (other.gameObject.CompareTag("Green"))
{
speed = -speed;
color = new Color(0, 255, 0);
}
if (other.gameObject.CompareTag("Indigo"))
{
speed = -speed;
color = new Color(75, 0, 130);
}
if (other.gameObject.CompareTag("Orange"))
{
speed = -speed;
color = new Color(255, 127, 0);
}
if (other.gameObject.CompareTag("Red"))
{
speed = -speed;
color = new Color(255, 0, 0);
}
if (other.gameObject.CompareTag("Violet"))
{
speed = -speed;
color = new Color(148, 0, 211);
}
if (other.gameObject.CompareTag("Yellow"))
{
speed = -speed;
color = new Color(255, 255, 0);
}
}
}
Comment
Best Answer
Answer by AntiBeta · Feb 26 at 03:03 PM
HI
you can try this.
if (other.gameObject.CompareTag("Colorable"))
{
speed = -speed;
color = other.gameObject.GetComponent<Marble>().color;
}
(ɔ◔‿◔)ɔ GL
Answer by YusufAfacan · Feb 26 at 03:35 PM
Thanks for your answer @AntiBeta. That sparked the solution in my mind.