Search Unity

Bug CS0200 Error when trying to move player gameobject

Discussion in 'Scripting' started by VeryGoodDeveloperISwear, Jan 8, 2023.

  1. VeryGoodDeveloperISwear

    VeryGoodDeveloperISwear

    Joined:
    Jan 7, 2023
    Posts:
    56
    I am getting a "error CS0200: Property or indexer 'GameObject.transform' cannot be assigned to -- it is read only" on line 18. What does this mean, and how can I fix it? I am trying to make it so that when the Player touches anything with this script as a component, it will be sent back to the start. Also, this in a 2D project.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerKill : MonoBehaviour
    6. {
    7.     public GameObject target;
    8.    
    9.     void Update()
    10.     {
    11.  
    12.     }
    13.    
    14.     private void OnCollisionEnter2D(Collision2D player)
    15.     {
    16.         if (player.gameObject.CompareTag("Player"))
    17.         {
    18.             target.transform = new Vector2 (0,3);
    19.         }
    20.     }
    21. }
    22.  
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,929
    .transform
    is a read-only property of type Transform. Meaning, it's not a Vector2, nor can it be assigned to.

    You probably mean to assign to it's
    .position
    property.
     
  3. VeryGoodDeveloperISwear

    VeryGoodDeveloperISwear

    Joined:
    Jan 7, 2023
    Posts:
    56
    Thank you, this fixed the bug