I
I
Ilya2021-05-05 15:50:02
Unity
Ilya, 2021-05-05 15:50:02

Why does the ball disappear?

I'm trying to make the ball at startup have a random color, the color changes (you can see it in the inspector), but the ball disappears completely,

public class PlayerController : MonoBehaviour
{
    public float jumpForce=10f;
    public Rigidbody2D rb;
    public string CurrentColor;
    public SpriteRenderer sr;
    public Color ColorGreen;
    public Color ColorBlue;
    public Color ColorRed;
    public Color ColorOrange;

    
    // Start is called before the first frame update
    void Start()
    {
        SetRandomColor();
        
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetButtonDown("Jump")|| Input.GetMouseButtonDown(0))
        {
            rb.velocity=Vector2.up* jumpForce;
        }
    }
   private void OnTriggerEnter2D(Collider2D other) {
       Debug.Log(other.tag);
   } 
 private void SetRandomColor()
   {
       int index=Random.Range(0,3);
       switch(index)
       {
           case 0:
                CurrentColor="green";
                   sr.color=ColorGreen;
                    break;
            case 1:
               CurrentColor="blue"; 
                        //sr.color=ColorBlue;
                        sr.color=ColorGreen;
                    break;
            case 2:
                    CurrentColor="red";
                       // sr.color=ColorRed;
                       sr.color=ColorGreen;
                    break;
            case 3:
                    CurrentColor="orange";
                        // sr.color=ColorOrange;
                        sr.color=ColorGreen;
            break;
       }
   }
}

60929475e86b1988928740.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GFX Data, 2021-05-05
@BeSnoMo

Your colors appear to be transparent. Give everyone an Alpha component = 255.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question