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

Cannot modify the return value of 'Transform.postion' because it is not a variable.

Discussion in 'Editor & General Support' started by KidCanada7, Jul 23, 2020.

  1. KidCanada7

    KidCanada7

    Joined:
    Jul 19, 2018
    Posts:
    13
    I'm trying to code in a script in my Unity3D game to have my Player lock in position until the timer ends, I'm really bad at coding so I've already tried looking for fixes but none seem to work, How do I do this properly without getting any errors like Cannot modify the return value of 'Transform.postion' because it is not a variable? (Also this script is attached to my Player)![alt text][1]

    upload_2020-7-22_19-3-17.png

    [1]: /storage/temp/163853-errorunity.png

    The code for the script is `using UnityEngine;
    using System.Collections;

    public class SimpleTimer : MonoBehaviour
    {

    public float targetTime = 22.0f;

    public void Update()
    {

    targetTime -= Time.deltaTime;

    if (targetTime <= 0.0f)
    {
    timerEnded();
    }

    if (targetTime <= 22.0f)
    {
    transform.position.x = 0.0f;
    transform.position.y = 0.0f;
    transform.position.z = 0.0f;
    }

    }

    void timerEnded()
    {
    transform.position.x = 1.0f;
    transform.position.y = 1.0f;
    transform.position.z = 1.0f;
    }


    }

    `
    I probably messed up this code so hard... ‍♂️
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    Instead of
    Code (CSharp):
    1. transform.position.x = 1.0f;
    2. transform.position.y = 1.0f;
    3. transform.position.z = 1.0f;
    use
    Code (CSharp):
    1. transform.position = Vector3.one;
    or
    Code (CSharp):
    1. Vector3.zero
     
  3. KidCanada7

    KidCanada7

    Joined:
    Jul 19, 2018
    Posts:
    13
    Does Vector3.one mean It's not locked and does Vector3.zero mean it is locked?
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    Vector3.zero is the same thing as
    new Vector3(0, 0, 0)
    and Vector3.one is the same as
    new Vector3(1, 1, 1)


    Your compile error though is because you have to replace the whole position at once, you can't just change one of the values like that.
     
  5. KidCanada7

    KidCanada7

    Joined:
    Jul 19, 2018
    Posts:
    13
    Okay, Well I'm wanting unlock the postion when the timer ends, How do I do that?
     
  6. KidCanada7

    KidCanada7

    Joined:
    Jul 19, 2018
    Posts:
    13
    Cause doing transform.position = Vector3.zero; just... well, Locks it in the zero position.
     
  7. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    Does your player have a Rigidbody attached?
     
  8. KidCanada7

    KidCanada7

    Joined:
    Jul 19, 2018
    Posts:
    13
    yes
     
  9. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    Try
    GetComponent<Rigidbody>.constraints = RigidbodyConstraints.FreezeAll
    and
    GetComponent<Rigidbody>.constraints = RigidbodyConstraints.None
     
  10. KidCanada7

    KidCanada7

    Joined:
    Jul 19, 2018
    Posts:
    13
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SimpleTimer : MonoBehaviour
    5. {
    6.  
    7.     public float targetTime = 22.0f;
    8.  
    9.     public void Update()
    10.     {
    11.  
    12.         targetTime -= Time.deltaTime;
    13.  
    14.         if (targetTime <= 0.0f)
    15.         {
    16.             timerEnded();
    17.         }
    18.  
    19.         if (targetTime <= 22.0f)
    20.         {
    21.             GetComponent<Rigidbody>.constraints = RigidbodyConstraints.FreezeAll;
    22.         }
    23.  
    24.     }
    25.  
    26.     void timerEnded()
    27.     {
    28.         GetComponent<Rigidbody>.constraints = RigidbodyConstraints.None;
    29.     }
    30.  
    31.  
    32. }
    33.  
    that my code now and I'm getting errors
     
  11. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    Sorry I made a typo:

    Try
    GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll
    and
    GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None
     
  12. KidCanada7

    KidCanada7

    Joined:
    Jul 19, 2018
    Posts:
    13
    Thanks, But the timer doesn't work which sucks... Thanks so much tho!
     
  13. KidCanada7

    KidCanada7

    Joined:
    Jul 19, 2018
    Posts:
    13
    fixed the timer