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

You are not connected. Please login or register

Unity Ninja Unity 3D Forum » HELP » Help (General) » tell me why this gives an error

tell me why this gives an error

View previous topic View next topic Go down  Message [Page 1 of 1]

1 tell me why this gives an error on Sat Aug 22, 2015 6:46 am

eric_cartman


Member [Lv. 1]
Member [Lv. 1]
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

View user profile

2 Re: tell me why this gives an error on Mon Aug 31, 2015 10:20 am

aquilinajake


Member [Lv. 3]
Member [Lv. 3]
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.

View user profile http://jakeaquilina.weebly.com/

3 Re: tell me why this gives an error on Mon Aug 31, 2015 4:30 pm

BryceCain25


SUPER MODERATOR
SUPER MODERATOR
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

View user profile

4 Re: tell me why this gives an error on Mon Aug 31, 2015 5:28 pm

aquilinajake


Member [Lv. 3]
Member [Lv. 3]
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!

View user profile http://jakeaquilina.weebly.com/

View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum