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. Join us on March 30, 2023, between 5 am & 1 pm EST, in the Performance Profiling Dev Blitz Day 2023 - Q&A forum and Discord where you can connect with our teams behind the Memory and CPU Profilers.
    Dismiss Notice

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:
    35
    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:
    4,008
    .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:
    35
    Thank you, this fixed the bug