Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Unity: Changing the alpha color of an object in random times(C#)

Discussion in 'Scripting' started by DeanAseraf1, Sep 30, 2019.

  1. DeanAseraf1

    DeanAseraf1

    Joined:
    Aug 3, 2018
    Posts:
    16
    Hello there,
    I'm trying to write a script to turn the lights off in my game at random times.
    the lights are just a 2d images with 0.4 alpha color.
    I wanted to change the alpha color in the sprite renderer to 0 every few seconds(random between 0-10) for 3 seconds and back to 0.4 again and start over.

    this is my code so far for the lights:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Lightning : MonoBehaviour {
    6.     private Color temp;
    7.  
    8.     void Start () {
    9.         temp = this.gameObject.GetComponent<SpriteRenderer>().color;
    10.         StartCoroutine(Waiter());
    11.  
    12.     }
    13.  
    14.     IEnumerator Waiter()
    15.     {
    16.         int wait_time = Random.Range(0, 10);
    17.         yield return new WaitForSecondsRealtime(wait_time);
    18.         temp.a = 0f;
    19.         yield return new WaitForSeconds(3);
    20.         temp.a = 0.4f;
    21.         StartCoroutine(Waiter());
    22.     }
    23. }
    for some reason nothing happens in the scene and there is no errors in the console ether...
    can anyone please tell me what I'm doing wrong here?
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    The sprite colour isn't a reference type, so you need to change a colour and then assign it. Try this;

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4. public class Lightning : MonoBehaviour
    5. {
    6.     private SpriteRenderer _sRenderer;
    7.     private WaitForSeconds _threeSecs = new WaitForSeconds(3f);
    8.  
    9.     private Color _spriteColour;
    10.     private float _originalalpha;
    11.  
    12.     private void Start()
    13.     {
    14.         _sRenderer = GetComponent<SpriteRenderer>();
    15.         StartCoroutine(Waiter());
    16.  
    17.         _spriteColour = _sRenderer.color;
    18.         _originalalpha = _spriteColour.a;
    19.     }
    20.  
    21.     IEnumerator Waiter()
    22.     {
    23.         while (true)
    24.         {
    25.             int wait_time = Random.Range(1, 11);
    26.  
    27.             yield return new WaitForSecondsRealtime(wait_time);
    28.  
    29.             _spriteColour.a = 0f;
    30.             _sRenderer.color = _spriteColour;
    31.  
    32.             yield return _threeSecs;
    33.  
    34.             _spriteColour.a = _originalalpha;
    35.             _sRenderer.color = _spriteColour;
    36.         }      
    37.     }
    38. }
     
  3. DeanAseraf1

    DeanAseraf1

    Joined:
    Aug 3, 2018
    Posts:
    16
    Thank you so much!