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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

SpriteRenderer.color - Can't make an object reference.

Discussion in 'Scripting' started by tarzanno, May 24, 2015.

  1. tarzanno

    tarzanno

    Joined:
    Apr 23, 2015
    Posts:
    58
    Hi!
    I'm trying to write a script that allows me to change the opacity of a sprite. I figured out it will be the fourth variable in the Color(1f,1f,1f,.5f), but I have an error:
    "an object reference is required to access non-static member" both for
    Color color = SpriteRenderer.GetComponent <Color>();
    and
    SpriteRenderer.color = new Color(1f,1f,1f,.5f);

    I know, that I have to make an object reference, but I don't know how to do this. I want that script to change the opacity of a sprite, it is attached to. I will make the alpha variable later.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Opacity : MonoBehaviour {
    5.  
    6.  
    7.     public    Color color;
    8.  
    9.  
    10.     void Awake () {
    11.             Color color = SpriteRenderer.GetComponent <Color>();
    12.         }
    13.    
    14.  
    15.     void Update () {
    16.                
    17.         SpriteRenderer.color = new Color(1f,1f,1f,.5f);
    18.     }
    19. }
    20.  
    Please help.
     
  2. Zenov

    Zenov

    Joined:
    Jul 28, 2014
    Posts:
    53
    You are declaring 'color' twice which you shouldn't, and you are trying to change the color inside a loop which means it will always be that color every frame from the start. You need some kind of event that will cause it to change, such as a mouse click.

    not sure exactly what you are trying to achieve but this would take a color in the inspector, assign it to the sprite renderer on start, then change the color on mouse-click
    Code (csharp):
    1.  
    2.     using UnityEngine;
    3.     using System.Collections;
    4.  
    5.     public class Opacity : MonoBehaviour {
    6.  
    7.         public Color color;
    8.         private SpriteRenderer sRenderer;
    9.  
    10.         void Start() {
    11.             sRenderer = GetComponent<SpriteRenderer>();
    12.             sRenderer.color = color;
    13.         }
    14.      
    15.         void Update () {
    16.             if (Input.GetMouseButtonDown(0))
    17.             {
    18.                 sRenderer.color = new Color(1f,1f,1f,.5f);
    19.             }
    20.         }
    21.     }
    22.  
     
    tarzanno and imosek8 like this.
  3. tarzanno

    tarzanno

    Joined:
    Apr 23, 2015
    Posts:
    58
    Thank you for your reply.
    I have two scripts:
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Opacity : MonoBehaviour {
    6.  
    7.  
    8.  
    9.     public    Color color;
    10.     private SpriteRenderer sRenderer;
    11.     PlayerHealth playerHealth;
    12.  
    13.     void Start () {
    14.         sRenderer = GetComponent<SpriteRenderer>();
    15.         sRenderer.color= color;
    16.          }
    17.  
    18.  
    19.     void Update () {
    20.         if (playerHealth.TakeDamage. ???)
    21.         {
    22.             for (float i=0f ; i<1f ; i--){
    23.         sRenderer.color = new Color(1f,1f,1f,i);
    24.     }
    25.         }
    26.     }
    27. }
    28.  
    29.  
    and

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class PlayerHealth : MonoBehaviour {
    6.     public  int startingHealth = 100;
    7.     public  int currentHealth;
    8.     public static int currentHealthAdd;
    9.     public Slider healthSlider;
    10.     public Image damageImage;
    11.     public AudioClip deathClip;
    12.     public float flashSpeed = 5f;
    13.     public Color flashColour = new Color(1f, 0f, 0f, 0.1f);
    14.  
    15.     Animator anim;
    16.     AudioSource playerAudio;
    17.     PlayerMobility playerMobility;
    18.     PlayerShooting playerShooting;
    19.     bool isDead;
    20.     bool damaged;
    21.  
    22. (...)
    23.  
    24. void Update()
    25.     {
    26.         healthSlider.value = currentHealth+currentHealthAdd;
    27.         //if the player has just been damaged...
    28.         if(damaged)
    29.         {
    30.             damageImage.color = flashColour;
    31.         }
    32.         else
    33.         {
    34.             damageImage.color = Color.Lerp (damageImage.color, Color.clear, flashSpeed * Time.deltaTime);
    35.         }
    36.         damaged = false;
    37.     }
    38.  
    39.     public void TakeDamage (int amount)
    40.     {
    41.         damaged = true; //błyskanie
    42.  
    43.         currentHealth -= amount;
    44.  
    45.         playerAudio.Play ();
    46.  
    47.         if(currentHealth <= 0 && !isDead)
    48.         {
    49.             Death ();
    50.         }
    51. }
    52.  
    53. (...)
    54. }

    I am trying to achieve reduction of opacity of a sprite, let's call it "Fog", when Player is taking damage (damaged = true).
    I don't know how do do this, and I'm wondering if the trigger of this script should be in a PlayerHealth script or in Opacity script (I think in PlayerHealth will result better performance).
     
    Last edited: May 24, 2015
  4. imosek8

    imosek8

    Joined:
    Apr 24, 2015
    Posts:
    10
    In
    Code (CSharp):
    1. void Update () {
    2.         if (PlayerHealth.TakeDamage.damaged=true)
    3.         {
    4.             for (float i=0f ; i<1f ; i--){
    5.            sRenderer.color = new Color(1f,1f,1f,i);
    6.                }
    7.          }
    8.     }
    Here in this condition should be "=" ? I
     
  5. tarzanno

    tarzanno

    Joined:
    Apr 23, 2015
    Posts:
    58
    This is the solution:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class PlayerHealth : MonoBehaviour {
    6.     public  int startingHealth = 100;
    7.     public  int currentHealth;
    8.     public static int currentHealthAdd;
    9.     public Slider healthSlider;
    10.     public Image damageImage;
    11.     public AudioClip deathClip;
    12.     public float flashSpeed = 5f;
    13.     public Color flashColour = new Color(1f, 0f, 0f, 0.1f);
    14.     public float amountAlpha = .1f;
    15.     Opacity opacity;
    16.  
    17.     Animator anim;
    18.     AudioSource playerAudio;
    19.     PlayerMobility playerMobility;
    20.     PlayerShooting playerShooting;
    21.     bool isDead;
    22.     bool damaged;
    23.  
    24.     void Awake ()
    25.     {
    26.         //Seeting up the references
    27.         anim = GetComponent <Animator> ();
    28.         playerAudio = GetComponent <AudioSource> ();
    29.         playerMobility = GetComponent <PlayerMobility> ();
    30.         playerShooting = GetComponentInChildren <PlayerShooting> ();
    31.  
    32.  
    33.         //setting up initial health
    34.         currentHealth = startingHealth;
    35.     }
    36.  
    37.     void Update()
    38.     {
    39.         healthSlider.value = currentHealth+currentHealthAdd;
    40.         //if the player has just been damaged...
    41.         if(damaged)
    42.         {
    43.             damageImage.color = flashColour;
    44.             Opacity.alpha += amountAlpha;
    45.         }
    46.         else
    47.         {
    48.             damageImage.color = Color.Lerp (damageImage.color, Color.clear, flashSpeed * Time.deltaTime);
    49.         }
    50.         damaged = false;
    51.     }
    52.  
    53.     public void TakeDamage (int amount)
    54.     {
    55.         damaged = true; //błyskanie
    56.  
    57.         currentHealth -= amount;
    58.  
    59.  
    60.         playerAudio.Play ();
    61.  
    62.         if(currentHealth <= 0 && !isDead)
    63.         {
    64.             Death ();
    65.         }
    66.     }
    67.     public void TakeHealth (int amountAdd)
    68.     {
    69.         currentHealth += amountAdd;
    70.  
    71.     }
    72.  
    73.  
    74.  

    and

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Opacity : MonoBehaviour {
    5.  
    6.  
    7.  
    8.     public    Color color;
    9.     private SpriteRenderer sRenderer;
    10.     PlayerHealth playerHealth;
    11.     public static float alpha;
    12.  
    13.     void Start () {
    14.         sRenderer = GetComponent<SpriteRenderer>();
    15.         sRenderer.color= color;
    16.         alpha = 0f;
    17.  
    18.     }
    19.    
    20.  
    21.     void Update () {
    22.    
    23.         sRenderer.color = new Color(1f,1f,1f,alpha);
    24.     }
    25.  
    26.     public void Alpha(int amountAlpha)
    27.             {
    28.                 alpha += amountAlpha;
    29.         }
    30.     }
    31.  
    Thanks guys for helping me!
     
    Last edited: May 24, 2015
  6. imosek8

    imosek8

    Joined:
    Apr 24, 2015
    Posts:
    10
    In if condition must be expression that returns bool value - it is true or false... I think you must check if the player is damaged to change the colour...
     
    tarzanno likes this.
  7. tarzanno

    tarzanno

    Joined:
    Apr 23, 2015
    Posts:
    58
    I posted a solution by editing previous post. Thanks!