Unity Ninja Unity 3D Forum
To have exclusive access to all downloads, recourses and links within this forum, a free registered account is required. Just a heads up Wink
Share
Go down
Member [Lv. 1]
Member [Lv. 1]
Posts : 3
Points : 1111
Rep : 0
Join date : 2015-07-15
View user profile

tell me why this gives an error

on Sat Aug 22, 2015 6:46 am
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
avatar
Member [Lv. 3]
Member [Lv. 3]
Posts : 45
Points : 1398
Rep : 3
Join date : 2015-08-30
Age : 23
Location : Melbourne
View user profilehttp://jakeaquilina.weebly.com/

Re: tell me why this gives an error

on Mon Aug 31, 2015 10:20 am
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.
avatar
SUPER MODERATOR
SUPER MODERATOR
Posts : 279
Points : 2142
Rep : 58
Join date : 2013-12-16
Age : 17
Location : Boise Idaho United States
View user profile

Re: tell me why this gives an error

on Mon Aug 31, 2015 4:30 pm
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
avatar
Member [Lv. 3]
Member [Lv. 3]
Posts : 45
Points : 1398
Rep : 3
Join date : 2015-08-30
Age : 23
Location : Melbourne
View user profilehttp://jakeaquilina.weebly.com/

Re: tell me why this gives an error

on Mon Aug 31, 2015 5:28 pm
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!
Back to top
Permissions in this forum:
You cannot reply to topics in this forum