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

why doesn’t this work?:

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

``````Vector3 position = transform.position;
position.z = target.rigidbody.position.y;
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.

Both. It return a copy of the Vector3 position, not the actual position.

2 Likes

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…

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?

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?

1 Like

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?

sorry i still need help with this, is it really easy to figure out?

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.

``````Vector3 pos;

void Update()
{
float delta = pos.y  - target.rigidbody.position.y;
Vector3 myPos = transform.position;
myPos.z += delta;
transform.position = myPos;
pos = target.rigidbody.position;
}
``````

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?

You could certainly encapsulate it in a method that takes two transform arguments if you wanted to. Entirely up to you.

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…

pos being a float makes absolutely no sense. But it would kind of make sense of you post your actual code…

1 Like

``````using UnityEngine;
using System.Collections;

public class MikeGreigJumpAltered : MonoBehaviour
{
public MikeGreigJumpAlteredPiece other;

Animator anim;

public Transform target;

public bool grounded = false;
public Transform groundCheck;

bool doubleJump = false;

void Start ()
{

anim = GetComponent<Animator>();

}

void FixedUpdate ()
{
bool grounded = Physics.CheckSphere(groundCheck.position, groundRadius, whatIsGround);
anim.SetBool("Ground", grounded);

if(grounded)

doubleJump = false;

float move = Input.GetAxis ("Horizontal");

anim.SetFloat("Speed", Mathf.Abs(move));

if(!grounded) return;

go.GetComponent(typeof(MikeGreigJumpAlteredPiece));

other.JumpDid();

Vector3 position = transform.position;
position.z = target.rigidbody.position.y;
transform.position = position;

}

void Update ()
{
if((grounded || !doubleJump) && Input.GetButton("Jump"))
{
anim.SetBool("Ground", false);
go.GetComponent(typeof(MikeGreigJumpAlteredPiece));

other.JumpDo();

Vector3 position = transform.position;
position.z = target.rigidbody.position.y;
transform.position = position;

if(!doubleJump&& !grounded)
doubleJump = true;
}
}

}
``````

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.

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:

``````using UnityEngine;
using System.Collections;

public class MikeGreigJumpAlteredPiece : MonoBehaviour
{

public float jumpForce = 400f;
public float maxSpeed = 0f;

void FixedUpdate()
{

//anim.SetFloat("Speed", Mathf.Abs(move));
}

public void JumpDo()
{

//rigidbody.velocity = new Vector2(move * maxSpeed, rigidbody.velocity.y);

}

public void JumpDid()
{
float move = Input.GetAxis ("Horizontal");

rigidbody.velocity = new Vector2(move * maxSpeed, rigidbody.velocity.y);
}
}
``````

What I meant is, make the code readable by getting rid of unneeded commented sections, but don’t remove the actual code.

okie dokie