Reputation: 21
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:
Change the color of the sphere to (blue or red).
To Start the sphere into a random color (blue or red).
If I can put a Hex color, It would be better, but if it a solid color, no problem.
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
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
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