Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.position'.

Discussion in 'Scripting' started by theRelation, Aug 18, 2014.

  1. theRelation

    theRelation

    Joined:
    Aug 18, 2014
    Posts:
    33
    why doesn't this work?:

    transform.position.z = target.rigidbody.position.y;
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Code (csharp):
    1. Vector3 position = transform.position;
    2. position.z = target.rigidbody.position.y;
    3. transform.position = position;
    I am too tired to think right now :)
    That is needed because Vector3 is a struct or because position is a property. I am sure someone will point it out.
     
  3. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Both. It return a copy of the Vector3 position, not the actual position.
     
    theRelation and Dantus like this.
  4. theRelation

    theRelation

    Joined:
    Aug 18, 2014
    Posts:
    33
    thanks so much! i'm struggling to grasp this geometry stuff. i'm trying to get a sprite on z to mirror a rigidbody on y by getting the rigidbody's last position on either update for fixedupdate-- like the rigidbody moves one step on y and the sprite moves on step on z. i hope the code doesn't look like i'm trying to get the sprite to meet the rigidbody in space. anything that explains how that code works would be helpful, i've been grimacing at the API for weeks...
     
  5. theRelation

    theRelation

    Joined:
    Aug 18, 2014
    Posts:
    33
    what if i wanted to multiply the degree the sprite's z position changes derived from the y of the rigidbody with a float? where would i put it in the equation offered?
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    It's not really clear what your goal is. Are you trying to move one object along Z the same amount that another object is moved along Y?
     
    theRelation likes this.
  7. theRelation

    theRelation

    Joined:
    Aug 18, 2014
    Posts:
    33
    haha, yes, that's it exactly, KelsoMRK. sorry for not being clear.

    EDIT: i noticed that with the code example Dantus gave when the rigidbody moves on y the amount of movement on z is only a slight nudge. how do i get it to actually move?
     
    Last edited: Aug 18, 2014
  8. theRelation

    theRelation

    Joined:
    Aug 18, 2014
    Posts:
    33
    sorry i still need help with this, is it really easy to figure out?
     
  9. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    You'll have to store the previous position and then determine how much it moved.

    Something like this should get you going in the right direction.

    Code (csharp):
    1.  
    2. Vector3 pos;
    3.  
    4. void Update()
    5. {
    6.     float delta = pos.y  - target.rigidbody.position.y;
    7.     Vector3 myPos = transform.position;
    8.     myPos.z += delta;
    9.     transform.position = myPos;
    10.     pos = target.rigidbody.position;
    11. }
    12.  
     
  10. theRelation

    theRelation

    Joined:
    Aug 18, 2014
    Posts:
    33
    thanks so much! i have this bad pattern of copying and pasting in code but not fully comprehending what the code does. planning to use this in a top down orthographic zelda-like game for mid-air things. i'm wondering if it would make sense to have this script in a main manager object and let other gameobjects use it? so i just paste this over the previous script?
     
  11. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    You could certainly encapsulate it in a method that takes two transform arguments if you wanted to. Entirely up to you.
     
  12. theRelation

    theRelation

    Joined:
    Aug 18, 2014
    Posts:
    33
    i stuffed it into the working method i had goin and the only way it stopped being red was if i declared "pos" as a float, by the "y" was still red, and i don't know what i'm doing. is there any documentation that explains all this, other than the API, that doesn't read like a barebones geometry text? i have no idea why declaring "pos" a float made it turn blue...
     
  13. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    pos being a float makes absolutely no sense. But it would kind of make sense of you post your actual code...
     
    theRelation likes this.
  14. theRelation

    theRelation

    Joined:
    Aug 18, 2014
    Posts:
    33
    lol you asked for it.



    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MikeGreigJumpAltered : MonoBehaviour
    5. {
    6.     public MikeGreigJumpAlteredPiece other;
    7.    
    8.  
    9.     Animator anim;
    10.    
    11.     public Transform target;
    12.  
    13.  
    14.     public bool grounded = false;
    15.     public Transform groundCheck;
    16.     float groundRadius = 0.2f;
    17.     public LayerMask whatIsGround;
    18.  
    19.  
    20.  
    21.     bool doubleJump = false;
    22.  
    23.  
    24.  
    25.  
    26.  
    27.  
    28. void Start ()
    29. {
    30.  
    31.     anim = GetComponent<Animator>();
    32.      
    33.     }
    34.  
    35.  
    36.  
    37.  
    38.  
    39. void FixedUpdate ()
    40. {
    41.     bool grounded = Physics.CheckSphere(groundCheck.position, groundRadius, whatIsGround);
    42. anim.SetBool("Ground", grounded);
    43.  
    44.     if(grounded)
    45.            
    46.         doubleJump = false;
    47.  
    48.         float move = Input.GetAxis ("Horizontal");
    49.      
    50.  
    51.     anim.SetFloat("Speed", Mathf.Abs(move));
    52.  
    53.  
    54.  
    55.     if(!grounded) return;
    56.  
    57.  
    58.  
    59.  
    60.  
    61.      go.GetComponent(typeof(MikeGreigJumpAlteredPiece));
    62.        
    63.      
    64.  
    65.  
    66.         other.JumpDid();
    67.        
    68.      
    69.        
    70.         Vector3 position = transform.position;
    71.         position.z = target.rigidbody.position.y;
    72.         transform.position = position;
    73.  
    74.  
    75.  
    76.  
    77. }
    78.  
    79. void Update ()
    80. {
    81.     if((grounded || !doubleJump) && Input.GetButton("Jump"))
    82.     {
    83.         anim.SetBool("Ground", false);
    84.            go.GetComponent(typeof(MikeGreigJumpAlteredPiece));
    85.          
    86.  
    87.  
    88.             other.JumpDo();
    89.            
    90.  
    91.             Vector3 position = transform.position;
    92.             position.z = target.rigidbody.position.y;
    93.             transform.position = position;
    94.  
    95.            
    96.  
    97.  
    98.         if(!doubleJump&& !grounded)
    99.             doubleJump = true;
    100.     }
    101. }
    102.  
    103. }
     
    Last edited: Aug 20, 2014
  15. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Sorry, but I can't find a logic in the code. You should definitely get rid of all the junk first of all. The comments make it extremely unreadable. Besides that I don't understand why you need a FixedUpdate, because you don't use any physics in there.
     
  16. theRelation

    theRelation

    Joined:
    Aug 18, 2014
    Posts:
    33
    it's only 1/2 the jump code mike geig teaches for 2d controllers, modified slightly.



    the comments in green are exempt from the code's logic, there just notes since my grasp of coding is shaky and i'm trying to remember what works and doesn't (it also offers a record of what i tried).

    the other half is on the rigidbody:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MikeGreigJumpAlteredPiece : MonoBehaviour
    5. {
    6.  
    7.     public float jumpForce = 400f;
    8.     public float maxSpeed = 0f;
    9.  
    10.     void FixedUpdate()
    11.     {
    12.  
    13.      
    14.         //anim.SetFloat("Speed", Mathf.Abs(move));
    15.     }
    16.  
    17.     public void JumpDo()
    18.     {
    19.         rigidbody.AddForce(new Vector3(0, jumpForce, 0));
    20.  
    21.         //rigidbody.velocity = new Vector2(move * maxSpeed, rigidbody.velocity.y);
    22.  
    23.     }
    24.  
    25.     public void JumpDid()
    26.     {
    27.         float move = Input.GetAxis ("Horizontal");
    28.      
    29.         rigidbody.velocity = new Vector2(move * maxSpeed, rigidbody.velocity.y);
    30.     }
    31. }
    32.  
     
  17. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    What I meant is, make the code readable by getting rid of unneeded commented sections, but don't remove the actual code.
     
  18. theRelation

    theRelation

    Joined:
    Aug 18, 2014
    Posts:
    33
    okie dokie