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. Dismiss Notice

Can't assign transitions

Discussion in 'Scripting' started by Temp10101, Feb 18, 2015.

  1. Temp10101

    Temp10101

    Joined:
    Feb 11, 2015
    Posts:
    54
    Assets/Scripts/Camera/CameraFollowup.cs(10,22): error CS0200: Property or indexer `UnityEngine.Component.transform' cannot be assigned to (it is read only)
    Yes, I know there's many tutorials for it. But most of them get answered for their specific situations, which either doesn't suit mine, or they provide final answers without explaining why it wasn't working, I have extremely easy script, here it is:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CameraFollowup : MonoBehaviour {
    5.  
    6.     public Transform Player;
    7.  
    8.     void Update() {
    9.         this.transform = Player;
    10.     }
    11. }
    12.  
    It is attached to camera, and has to follow player, without following it's angles. But I get error which I mentioned first, tried Vector3 and GameObject too. Maybe I should do something else?
     
  2. vintar

    vintar

    Joined:
    Sep 18, 2014
    Posts:
    90
    I did something similar yesterday :
    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. class CameraFollow : MonoBehaviour
    5. {
    6.      private Vector3 startPos;
    7.      public GameObject player;
    8.      float zDistance;
    9.  
    10.      void Start()
    11.      {
    12.          startPos = transform.position;
    13.          zDistance = startPos.z - player.transform.position.z;
    14.      }
    15.  
    16.      void Update()
    17.      {
    18.          Vector3 newPos = new Vector3(player.transform.position.x, startPos.y, player.transform.position.z + zDistance);
    19.          transform.position = newPos;
    20.      }
    21. }
     
    akast07 and Temp10101 like this.
  3. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    I don't think you can assign transforms like this. I think you can try instead :

    this.gameObject.transform= Player;

    or if the above don't work, how about this :

    this.gameObject.transform.position = Player.position;

    or this :

    this.transform.position = Player.posistion;

    Too many choices to try :D
     
  4. Temp10101

    Temp10101

    Joined:
    Feb 11, 2015
    Posts:
    54
    Thank you, changed code a bit and it worked for my purposes.