FragMate CommunityLog in

FragMate - Software and Game Developer Community, including Resources, Forums, Marketplace & More.

Share

descriptiontell me why this gives an error

more_horiz
using UnityEngine;
using System.Collections;

public class playerMovement : MonoBehaviour {
public float paddleSpeed = 1f;
public Vector3 playerPos = new Vector3();




// Update is called once per frame
void Update () {

float ypos = gameObject.transform.position.x + Input.GetAxis("Horizontal") * paddleSpeed;
gameObject.transform.position.x = ypos;




}
}






the above code seems perfectly fine to me yet it shows some errors saying to use temporary variable for gameObject.transform.position.x = ypos;
i just cant understand? would really apreciate if you guys explained me. i am kind of new to unity and new to game coding. so bear with me lol

descriptionRe: tell me why this gives an error

more_horiz
Hello there!

Firstly, i assume you are using C#, please specify in title this would greatly help to assist with the issue.

i agree your code does look fine, however when i tested it, it did seem off. so firstly what it is saying is you can not modify the (gameObject.transform.position.x) directly, meaning you can not edit just the position.x value and nothing else, this is possible in javascript i believe, but in c# you need to edit the entire position.

replace gameObject.transform.position.x = ypos;
with gameObject.transform.position = new Vector3(ypos, 0, 0);


you may need to move the ypos around in the Vector3 to get what you are after, as i dont know what you are trying to do i assume what i have provided will be enough to help you on your way, if not please reply back with more information so i can assist further.

_________________
https://www.youtube.com/channel/UCrS-nC8CYukMmhAKpyq6TCA

descriptionRe: tell me why this gives an error

more_horiz
@aquilinajake wrote:
Hello there!

Firstly, i assume you are using C#, please specify in title this would greatly help to assist with the issue.

i agree your code does look fine, however when i tested it, it did seem off. so firstly what it is saying is you can not modify the  (gameObject.transform.position.x) directly, meaning you can not edit just the position.x value and nothing else, this is possible in javascript i believe, but in c# you need to edit the entire position.

replace  gameObject.transform.position.x = ypos;
with       gameObject.transform.position = new Vector3(ypos, 0, 0);  


you may need to move the ypos around in the Vector3 to get what you are after, as i dont know what you are trying to do i assume what i have provided will be enough to help you on your way, if not please reply back with more information so i can assist further.
Awesome explanation aquilinajake! I would have to 100% agree. In JavaScript all you need to do is just replace the x position with the ypos variable. Seems like C# has more variability though, once again awesome explanation Very Happy

_________________
-BryceCain25

descriptionRe: tell me why this gives an error

more_horiz
No worries bud glad to be of an assistance, if there is anything else just let me know or post here, I found this community small and I like it so I might stick around to help out abit, you guys seem peetty cool and I don't think I've seen a trol yet over the past few days. Good luck with the script/game!

_________________
https://www.youtube.com/channel/UCrS-nC8CYukMmhAKpyq6TCA
Permissions in this forum:
You cannot reply to topics in this forum