Osama Monzer
Osama Monzer

Reputation: 21

How to change the color of a sphere after crossing a cube in Unity 3D?

I am new Unity3D, I really love it, and when I started my project, I have a problem, I have a sphere which it moves by (AddForce), and I put a Material to it which is (red), I want from that sphere after crossing a cube which it is on (isTrigger) mode and not visible is:

  1. Change the color of the sphere to (blue or red).

  2. To Start the sphere into a random color (blue or red).

  3. If I can put a Hex color, It would be better, but if it a solid color, no problem.

  4. If the random color = to cube 1 color it will continue moving, else: random color != to cube 1 it will stop moving, you could say that it is an obstacle.

I really believe that it wants coding, but I only know Python, so if you can write for me the code to understand it better.

Upvotes: 0

Views: 1757

Answers (2)

Blazkowicz
Blazkowicz

Reputation: 195

The color of your sphere is defined by the matelial you applied on it.

var sphere = gameObject.GetComponent<Renderer>();
sphere.material.SetColor("_Color", Color.red);

Upvotes: 1

Bartek Dusza
Bartek Dusza

Reputation: 174

Here is my code example for random color

void Start()
{
    int a=Random.Range(0,2);
    var sphere =GetComponent<Renderer>();
    sphere.material.SetColor("_Color",a==0?Color.Blue:Color.Red);
}

This should solve the random color problem

Upvotes: 0

Related Questions