Search Unity

`UnityEngine.Component.camera' cannot be assigned to (it is read only)

Discussion in 'Scripting' started by GreenBoxInteractive, May 12, 2015.

  1. GreenBoxInteractive

    GreenBoxInteractive

    Joined:
    Mar 23, 2015
    Posts:
    135
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class player : MonoBehaviour {
    5.  
    6.     public int smoothTime;
    7.  
    8.     public Color purple;
    9.     public Color currentColour;
    10.  
    11.    
    12.     void Awake () {
    13.         camera = GetComponent<Camera>();
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void Update () {
    18.         currentColour = camera.backgroundColor;
    19.     }
    20.  
    21.     void OnTriggerEnter2D(Collider2D bar) {
    22.         if (bar.gameObject.tag == "purple") {
    23.             camera.backgroundColor = Color.Lerp(currentColour, purple, Time.deltaTime * smoothTime);
    24.         }
    25.     }
    26. }
    27.  
    I get this error:

    Assets/Scripts/player.cs(14,17): error CS0200: Property or indexer `UnityEngine.Component.camera' cannot be assigned to (it is read only)


    I understand it, but don't get how to fix it, any help
     
  2. Fajlworks

    Fajlworks

    Joined:
    Sep 8, 2014
    Posts:
    344
    You can create a public variable and assign it via inspector or using GetComponent<Camera>:
    Code (CSharp):
    1. public class player : MonoBehaviour
    2. {
    3.    public int smoothTime;
    4.    public Color purple;
    5.    public Color currentColour;
    6.  
    7.    public Camera myCamera;    //<-- var to store camera component reference
    8.  
    9.     void Awake ()
    10.     {
    11.         myCamera = GetComponent<Camera>();
    12.     }
    13.  
    14.     void OnTriggerEnter2D(Collider2D bar)
    15.     {
    16.          if (bar.gameObject.tag == "purple")
    17.          {
    18.                 myCamera.backgroundColor = Color.Lerp(currentColour, purple, Time.deltaTime * smoothTime);
    19.          }
    20.     }
    21. }
    22.  
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Incidentally, that line is unnecessary. camera is readonly because it's an accessor function... and that accessor function is a shorthand for GetComponent<Camera>().

    This is essentially what your compiler sees when it compiles that line:
    Code (csharp):
    1. GetComponent<Camera>() = GetComponent<Camera>();
    Fajiworks's suggestion is better in general, because caching component references you use repeatedly will be faster.

    (Note: this applies to 4.x and before. I believe the camera property was removed along with most of the convenience accessors like it in 5.0.)
     
    GreenBoxInteractive likes this.
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The camera property was made obsolete, but it's still there (so Unity can tell you it's obsolete). Rename "camera" to something else.

    --Eric